Ticket #20740: pass-protocol.diff

File pass-protocol.diff, 1.6 KB (added by Jeff250, 2 years ago)

Pass protocol in formfield() method plus tests

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 02d887b..a7e0b52 100644
    a b class GenericIPAddressField(Field): 
    13341334        return value
    13351335
    13361336    def formfield(self, **kwargs):
    1337         defaults = {'form_class': forms.GenericIPAddressField}
     1337        defaults = {
     1338            'protocol': self.protocol,
     1339            'form_class': forms.GenericIPAddressField,
     1340        }
    13381341        defaults.update(kwargs)
    13391342        return super(GenericIPAddressField, self).formfield(**defaults)
    13401343
  • tests/model_fields/tests.py

    diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
    index 6546c49..049d77d 100644
    a b class BinaryFieldTests(test.TestCase): 
    468468    def test_max_length(self):
    469469        dm = DataModel(short_data=self.binary_data*4)
    470470        self.assertRaises(ValidationError, dm.full_clean)
     471
     472class GenericIPAddressFieldTests(test.TestCase):
     473    def test_genericipaddressfield_formfield_protocol(self):
     474        """
     475        Test that GenericIPAddressField with a specified protocol does not
     476        generate a formfield with no specified protocol.  See #20740.
     477        """
     478        model_field = models.GenericIPAddressField(protocol='IPv4')
     479        form_field = model_field.formfield()
     480        self.assertRaises(ValidationError, form_field.clean, '::1')
     481        model_field = models.GenericIPAddressField(protocol='IPv6')
     482        form_field = model_field.formfield()
     483        self.assertRaises(ValidationError, form_field.clean, '127.0.0.1')
Back to Top