diff options
| author | Tim Graham <timograham@gmail.com> | 2013-07-12 16:05:14 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-07-12 16:05:14 -0400 |
| commit | f2cb94f1c0dc98663e7d493411bf9b82d1894559 (patch) | |
| tree | cdf7a20d2cd5c023b4f5237dab4e811fd9560e8a | |
| parent | a7d97a6778ca528199920901ed2f66e289ee5a44 (diff) | |
Fixed #20740 -- GenericIPAddressField should pass protocol to formfield()
Thanks Jeff250.
| -rw-r--r-- | django/db/models/fields/__init__.py | 5 | ||||
| -rw-r--r-- | tests/model_fields/tests.py | 13 |
2 files changed, 17 insertions, 1 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 02d887b7f8..a7e0b52e87 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1334,7 +1334,10 @@ class GenericIPAddressField(Field): return value def formfield(self, **kwargs): - defaults = {'form_class': forms.GenericIPAddressField} + defaults = { + 'protocol': self.protocol, + 'form_class': forms.GenericIPAddressField, + } defaults.update(kwargs) return super(GenericIPAddressField, self).formfield(**defaults) diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index 6546c49ec7..1ce5eba3e9 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -468,3 +468,16 @@ class BinaryFieldTests(test.TestCase): def test_max_length(self): dm = DataModel(short_data=self.binary_data*4) self.assertRaises(ValidationError, dm.full_clean) + +class GenericIPAddressFieldTests(test.TestCase): + def test_genericipaddressfield_formfield_protocol(self): + """ + Test that GenericIPAddressField with a specified protocol does not + generate a formfield with no specified protocol. See #20740. + """ + model_field = models.GenericIPAddressField(protocol='IPv4') + form_field = model_field.formfield() + self.assertRaises(ValidationError, form_field.clean, '::1') + model_field = models.GenericIPAddressField(protocol='IPv6') + form_field = model_field.formfield() + self.assertRaises(ValidationError, form_field.clean, '127.0.0.1') |
