Opened 10 years ago
Closed 10 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 , 10 years ago
Easy pickings: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 10 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
comment:4 by , 10 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:5 by , 10 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:6 by , 10 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
PR