Opened 5 years ago

Closed 5 years ago

Last modified 4 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@…> 5 years ago.
django-model-choice-field-validation.2.diff (2.3 KB) - added by Petr Marhoun <petr.marhoun@…> 5 years ago.
12510-against-12104.diff (2.7 KB) - added by Honza_Kral 5 years ago.
Changed tests to UnitTest, simplified them a bit.

Download all attachments as: .zip

Change History (8)

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

comment:1 Changed 5 years ago by Honza_Kral

  • milestone set to 1.2
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Thanks, could you provide some tests for this behavior?

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

comment:2 Changed 5 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 5 years ago by Honza_Kral

Changed tests to UnitTest, simplified them a bit.

comment:3 Changed 5 years ago by Honza_Kral

  • Needs tests unset

Added a simpler test case in unittest format...

comment:4 Changed 5 years ago by jkocherhans

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

(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 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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