mirror of
				https://github.com/django/django.git
				synced 2025-10-26 07:06:08 +00:00 
			
		
		
		
	Fixed #20740 -- GenericIPAddressField should pass protocol to formfield()
Thanks Jeff250.
This commit is contained in:
		| @@ -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) | ||||
|  | ||||
|   | ||||
| @@ -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') | ||||
|   | ||||
		Reference in New Issue
	
	Block a user