Opened 9 years ago

Closed 9 years ago

#24708 closed Bug (fixed)

forms.GenericIPAddressField.to_python behaves differently to other CharField subclasses.

Reported by: Keryn Knight Owned by: Pradeek
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: django@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Other forms.fields.CharField subclasses (UUIDField, URLField), when they replace the to_python method, call super(..., self).to_python(...) before acting on the value.

GenericIPAddressField however, does not, which means it calls .strip() on the input, assuming the input is a string.

Thus, the following fails:

>>> GenericIPAddressField().to_python(1)
AttributeError: 'int' object has no attribute 'strip'

where the other aforementioned subclasses succeed in their functionality:

>>> URLField().to_python(1)
'http://1'
>>> UUIDField().to_python(1)
ValidationError: ['Enter a valid UUID.']

Because the call to super(...) ensures either '' or smart_text(...) is returned.

I think the fix is thus:

    def to_python(self, value):
        value = super(GenericIPAddressField, self).to_python(value).strip()  # this is the fix line.
        if value in self.empty_values:
            return ''
        if value and ':' in value:
            return clean_ipv6_address(value, self.unpack_ipv4)
        return value

Change History (7)

comment:1 by Tim Graham, 9 years ago

Easy pickings: set
Triage Stage: UnreviewedAccepted

comment:2 by Merouane Atig, 9 years ago

Owner: changed from nobody to Merouane Atig
Status: newassigned

comment:3 by Tim Graham, 9 years ago

Has patch: set
Patch needs improvement: set

comment:4 by Merouane Atig, 9 years ago

Owner: Merouane Atig removed
Status: assignednew

comment:5 by Pradeek, 9 years ago

Owner: set to Pradeek
Status: newassigned

comment:6 by Tim Graham, 9 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:7 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 6123e613:

Fixed #24708 -- Handled non-string values in GenericIPAddressField.to_python()

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