Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#12510 closed (fixed)

ModelChoiceField.clean call to_python and validate from ChoiceField

Reported by: Petr Marhoun <petr.marhoun@…> Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Comment in ModelChoiceField says: This class is a subclass of ChoiceField for purity, but it doesn't actually use any of ChoiceField's implementation.

But with model level validation method ModelChoiceField.clean calls Field.clean and it calls self.to_python and self.validate, in reality ChoiceField.to_python and ChoiceField.validate. And for ModelChoiceField these methods are useless and can be harmful - for example (my problem) with hidden ModelChoiceField choices were not created but they are now, it could be very ineffective.

Attachments (3)

django-model-choice-field-validation.diff (963 bytes) - added by Petr Marhoun <petr.marhoun@…> 7 years ago.
django-model-choice-field-validation.2.diff (2.3 KB) - added by Petr Marhoun <petr.marhoun@…> 7 years ago.
12510-against-12104.diff (2.7 KB) - added by Honza Král 7 years ago.
Changed tests to UnitTest, simplified them a bit.

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by Petr Marhoun <petr.marhoun@…>

comment:1 Changed 7 years ago by Honza Král

milestone: 1.2
Needs tests: set
Triage Stage: UnreviewedAccepted

Thanks, could you provide some tests for this behavior?

Changed 7 years ago by Petr Marhoun <petr.marhoun@…>

comment:2 Changed 7 years ago by Petr Marhoun <petr.marhoun@…>

I attached test - it is quite artificial ("problem" in test could be solved with simple "select_related" in queryset), but it should prove that choices are really generated.

Changed 7 years ago by Honza Král

Attachment: 12510-against-12104.diff added

Changed tests to UnitTest, simplified them a bit.

comment:3 Changed 7 years ago by Honza Král

Needs tests: unset

Added a simpler test case in unittest format...

comment:4 Changed 7 years ago by jkocherhans

Resolution: fixed
Status: newclosed

(In [12211]) Fixed #12510. Changed ModelChoiceField to stop using some of its superclasses implementation. This could cause more than one query when generating choices. Thanks, Petr Marhoun and Honza Kral.

comment:5 Changed 5 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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