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):
|
1334 | 1334 | return value |
1335 | 1335 | |
1336 | 1336 | def formfield(self, **kwargs): |
1337 | | defaults = {'form_class': forms.GenericIPAddressField} |
| 1337 | defaults = { |
| 1338 | 'protocol': self.protocol, |
| 1339 | 'form_class': forms.GenericIPAddressField, |
| 1340 | } |
1338 | 1341 | defaults.update(kwargs) |
1339 | 1342 | return super(GenericIPAddressField, self).formfield(**defaults) |
1340 | 1343 | |
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index 6546c49..049d77d 100644
a
|
b
|
class BinaryFieldTests(test.TestCase):
|
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') |