Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#18431 closed Bug (invalid)

TypedChoiceField doesn't use self.coerce in its clean() and prepare_value() methods

Reported by: Łukasz Langa Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


One of the points of the clean() method is to clean the data to the correct type. When a coerce function is defined for a TypedChoiceField, it should then be used by clean() and prepare_value() as well.

Change History (3)

comment:1 Changed 4 years ago by err

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Clean is returning coerced value. Because clean is calling to_python method.

comment:2 Changed 4 years ago by anonymous

You're right. coerce is indeed invoked in TypedChoiceField.to_python(). The problem however is with the fact that this method first calls ChoiceField.to_python() which blatantly runs smart_unicode(value) regardless of whether that's compatible with coerce or not. Moreover, the following line in TypedChoiceField.to_python() reads:

super(TypedChoiceField, self).validate(value)

which means it skips TypedChoiceField.validate() and runs the super ChoiceField.validate() instead. Is that conscious design or is it a bug?

Also, note that prepare_value() does not use coerce at all.

comment:3 Changed 4 years ago by Aymeric Augustin

Resolution: invalid
Status: newclosed

You're describing a very high level issue with the design of Django's forms. This doesn't lead to any clear, actionnable items. As a consequence the ticket has languished here for five months without feedback.

I suggest taking this topic to the django-developers mailing list. You'll reach a wider audience and it'll be a better medium for the discussion. You should give more details; try to frame the problem and explain your proposal.

If the discussion determines that Django's behavior should be changed — taking into account backward-compatibility requirements — then you're welcome to add details to this ticket and reopen it, or to open a new one.

Last edited 4 years ago by Aymeric Augustin (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top