Opened 17 years ago

Last modified 14 years ago

#5327 closed

ChoiceField clean method — at Initial Version

Reported by: danielrubio Owned by: nobody
Component: Documentation Version: dev
Severity: Keywords: ChoiceField clean method id
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using both ChoiceField and ModelChoiceField, I discovered a bug in ChoiceField clean method ( or a discrepancy in behaviour)

ModelChoiceField seems to be working as expected, when I call the clean method in a template like so: form.clean.city , I get the city name(e.g New York), or what would be string inside the tag <select id="5"> New York </select>

This behaviour is different if the values are inside a ChoiceField, if I use the following in the template: form.clean.city, I get the city id (e.g 5 ), not the expected string or behaviour as using ModelChoiceField.

[NOTE: Not calling the clean field, in either ChoiceField or ModelChoiceField works identcally, generating a select list ]

I modified the fields.py file in django/newforms, the clean method on the ChoiceField class[line 466], would now read:

def clean(self, value):

"""
Validates that the input is in self.choices.
"""
value = super(ChoiceField, self).clean(value)
if value in EMPTY_VALUES:

value = u

value = smart_unicode(value)
if value == u:

return value

valid_values = set([smart_unicode(k) for k, v in self.choices])
if value not in valid_values:

raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))

else:

value = self._choices[int(value)][1]

return value

Only modification is the 'else' at the end, which would assign the value to the actual string.


Change History (0)

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