Code

Opened 23 months ago

Closed 17 months ago

Last modified 17 months ago

#18431 closed Bug (invalid)

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

Reported by: ambv 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

Description

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.

Attachments (0)

Change History (3)

comment:1 Changed 22 months 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 22 months 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 17 months ago by aaugustin

  • Resolution set to invalid
  • Status changed from new to closed

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 17 months ago by aaugustin (previous) (diff)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.