GenericIPAddressField does not pass protocol to formfield()
|Reported by:||Jeff250||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
If we have a model containing a GenericIPAddressField specifying a protocol...
class MyModel(models.Model): host = models.GenericIPAddressField(protocol='IPv4') . . .
And a ModelForm wrapping it...
class MyForm(forms.ModelForm): class Meta: model = models.MyModel . . .
Then that form can give a misleading error message after validation. For instance, with the example above, if you input, e.g., "bletch", you get the error message "Enter a valid IPv4 or IPv6 address", even though IPv6 addresses are not allowed. (However, if you input, e.g., "::1", you get the accurate message "Enter a valid IPv4 address".)
It seems like GenericIPAddressField doesn't pass its protocol in its formfield() method, so the GenericIPAddressField form field first uses a more general validation rule than the GenericIPAddressField model field. I'm attaching a patch which fixes this issue in my application.
Change History (8)
comment:1 Changed 23 months ago by timo
- Cc timograham@… added
- Component changed from Uncategorized to Forms
- Easy pickings set
- Has patch set
- Needs documentation unset
- Needs tests set
- Patch needs improvement unset
- Summary changed from GenericIPAddressField gives misleading error in ModelForm to GenericIPAddressField does not pass protocol to formfield()
- Triage Stage changed from Unreviewed to Accepted
- Type changed from Uncategorized to Bug
Changed 23 months ago by Jeff250
comment:3 Changed 23 months ago by Tim Graham <timograham@…>
- Resolution set to fixed
- Status changed from new to closed