Opened 17 years ago
Closed 14 years ago
#5481 closed (wontfix)
ChoiceField/ModelChoiceField always returns strings, regardless of type in choices
Reported by: | Robert Coup | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | sprint14sep | |
Cc: | philippe.raoult@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
f = ChoiceField(choices=((1,'1'), (2,'2'))) f.clean('1') u'1' # expected: 1 (integer)
While normally this doesn't cause problems (models are pretty good about accepting strings) it can be extra hassle for custom forms.
The attached patch changes the behaviour of ChoiceField and ModelChoiceField to return exactly what was specified in choices (be it integer, string, unicode, whatever).
Attachments (3)
Change History (14)
by , 17 years ago
Attachment: | choicefield_correct_type.diff added |
---|
comment:1 by , 17 years ago
by , 17 years ago
Attachment: | choicefield_correct_type.2.diff added |
---|
a slightly cleaner implementation
comment:2 by , 17 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
comment:3 by , 17 years ago
I'm not comfortable with this change. We have a policy that whenever a string is involved, be it a UTF-8 bytestring or a Unicode string, Django will convert it a unicode object for internal use. This is breaking out of that policy by trying to preserve the str type. It's a bit inconsistent.
Is it satisfactory to pass strings_only=True to smart_unicode() instead of removing the smart_unicode() calls? That would make me happier.
comment:4 by , 17 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
comment:6 by , 17 years ago
#5247 isn't a duplicate - this ticket is related to how the choices are evaluated in ChoiceField.clean()
, the other ticket is related to how the choices get evaluated for display in the widget render method.
comment:7 by , 17 years ago
Status: | new → assigned |
---|
I hadn't seen strings_only
before, but i have some reservations about it - namely, it should probably be called ignore_none_and_int
. That prevents objects, doubles, booleans, Decimals, etc as being keys.
Would doing isinstance(value, str)
and unicode'ing only those be acceptable? (Patch .3 does it this way)
comment:10 by , 15 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:11 by , 14 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
r14829 added a TypedMultipleChoiceField, making this a wontfix.
For the archives, originally kicked off from #3729