Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
Tim Graham authored July 12, 2013
5  django/db/models/fields/__init__.py
@@ -1160,7 +1160,10 @@ def get_prep_value(self, value):
1160 1160
         return value
1161 1161
 
1162 1162
     def formfield(self, **kwargs):
1163  
-        defaults = {'form_class': forms.GenericIPAddressField}
  1163
+        defaults = {
  1164
+            'protocol': self.protocol,
  1165
+            'form_class': forms.GenericIPAddressField,
  1166
+        }
1164 1167
         defaults.update(kwargs)
1165 1168
         return super(GenericIPAddressField, self).formfield(**defaults)
1166 1169
 
13  tests/model_fields/tests.py
@@ -468,3 +468,16 @@ def test_set_and_retrieve(self):
468 468
     def test_max_length(self):
469 469
         dm = DataModel(short_data=self.binary_data*4)
470 470
         self.assertRaises(ValidationError, dm.full_clean)
  471
+
  472
+class 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')

0 notes on commit 45fab74

Please sign in to comment.
Something went wrong with that request. Please try again.