#20740 closed Bug (fixed)
GenericIPAddressField does not pass protocol to formfield()
| Reported by: | Jeffrey Knockel | Owned by: | nobody |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | timograham@… | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | yes | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
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.
Attachments (2)
Change History (8)
comment:1 by , 12 years ago
| Cc: | added |
|---|---|
| Component: | Uncategorized → Forms |
| Easy pickings: | set |
| Has patch: | set |
| Needs tests: | set |
| Summary: | GenericIPAddressField gives misleading error in ModelForm → GenericIPAddressField does not pass protocol to formfield() |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
by , 12 years ago
| Attachment: | pass-protocol.diff added |
|---|
Pass protocol in formfield() method plus tests
comment:3 by , 12 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
by , 12 years ago
| Attachment: | backport-1.6.x.diff added |
|---|
Additional patch required for 1.6.x backport
comment:5 by , 12 years ago
Tim, the 1.6.x backport isn't quite right, since the 1.6.x constructor doesn't already set self.protocol. I've attached a trivial patch against 1.6.x that should fix it.
Could you add a test for this?