Skip to content

Commit

Permalink
Fixed #20740 -- GenericIPAddressField should pass protocol to formfie…
Browse files Browse the repository at this point in the history
…ld()

Thanks Jeff250.
  • Loading branch information
timgraham committed Jul 12, 2013
1 parent a7d97a6 commit f2cb94f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion django/db/models/fields/__init__.py
Expand Up @@ -1334,7 +1334,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)

Expand Down
13 changes: 13 additions & 0 deletions tests/model_fields/tests.py
Expand Up @@ -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')

0 comments on commit f2cb94f

Please sign in to comment.