Opened 17 years ago

Closed 13 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)

choicefield_correct_type.diff (5.2 KB ) - added by Robert Coup 17 years ago.
choicefield_correct_type.2.diff (5.5 KB ) - added by Robert Coup 17 years ago.
a slightly cleaner implementation
choicefield_correct_type.3.diff (4.6 KB ) - added by Robert Coup 17 years ago.
Forces strings to unicode

Download all attachments as: .zip

Change History (14)

by Robert Coup, 17 years ago

comment:1 by Robert Coup, 17 years ago

For the archives, originally kicked off from #3729

by Robert Coup, 17 years ago

a slightly cleaner implementation

comment:2 by Simon G. <dev@…>, 17 years ago

Triage Stage: UnreviewedReady for checkin

comment:3 by Malcolm Tredinnick, 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 Malcolm Tredinnick, 17 years ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:5 by Philippe Raoult, 17 years ago

Cc: philippe.raoult@… added
Owner: changed from nobody to Robert Coup

likely related to #5247.

comment:6 by Robert Coup, 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 Robert Coup, 17 years ago

Status: newassigned

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)

by Robert Coup, 17 years ago

Forces strings to unicode

comment:8 by evenrik, 15 years ago

Use the new TypedChoiceField to solve this issue.

comment:9 by klrfin@…, 15 years ago

There's no TypedMultipleChoiceField.

comment:10 by Robert Coup, 14 years ago

Owner: changed from Robert Coup to nobody
Status: assignednew

comment:11 by Russell Keith-Magee, 13 years ago

Resolution: wontfix
Status: newclosed

r14829 added a TypedMultipleChoiceField, making this a wontfix.

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