Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.6.x] Fixed #20740 -- GenericIPAddressField should pass protocol to…

… formfield()

Thanks Jeff250.

Backport of f2cb94f from master
  • Loading branch information...
commit 45fab74561a2b339cde922c196ab3ccf998bad10 1 parent b30a872
@timgraham timgraham authored
View
5 django/db/models/fields/__init__.py
@@ -1160,7 +1160,10 @@ def get_prep_value(self, value):
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)
View
13 tests/model_fields/tests.py
@@ -468,3 +468,16 @@ def test_set_and_retrieve(self):
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')
Please sign in to comment.
Something went wrong with that request. Please try again.