Opened 23 months ago

Closed 23 months ago

Last modified 23 months ago

#20740 closed Bug (fixed)

GenericIPAddressField does not pass protocol to formfield()

Reported by: Jeff250 Owned by: nobody
Component: Forms Version: master
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)

pass-protocol.diff (1.6 KB) - added by Jeff250 23 months ago.
Pass protocol in formfield() method plus tests
backport-1.6.x.diff (660 bytes) - added by Jeff250 23 months ago.
Additional patch required for 1.6.x backport

Download all attachments as: .zip

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

Could you add a test for this?

Changed 23 months ago by Jeff250

Pass protocol in formfield() method plus tests

comment:2 Changed 23 months ago by Jeff250

Sure, I've just attached a patch including tests.

comment:3 Changed 23 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In f2cb94f1c0dc98663e7d493411bf9b82d1894559:

Fixed #20740 -- GenericIPAddressField should pass protocol to formfield()

Thanks Jeff250.

comment:4 Changed 23 months ago by Tim Graham <timograham@…>

In 45fab74561a2b339cde922c196ab3ccf998bad10:

[1.6.x] Fixed #20740 -- GenericIPAddressField should pass protocol to formfield()

Thanks Jeff250.

Backport of f2cb94f1c0 from master

Changed 23 months ago by Jeff250

Additional patch required for 1.6.x backport

comment:5 Changed 23 months ago by Jeff250

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.

comment:6 Changed 23 months ago by timo

Thanks Jeff.

Note: See TracTickets for help on using tickets.
Back to Top