Changes between Initial Version and Version 1 of Ticket #32244, comment 8


Ignore:
Timestamp:
Dec 8, 2020, 1:51:09 PM (4 years ago)
Author:
Lushen Wu

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #32244, comment 8

    initial v1  
    55Currently the validation callstack goes BaseForm.full_clean() -> BaseForm._clean_fields(value) -> Field.clean(value) -> ModelChoiceField.to_python(value).
    66
    7 From what I can see, ModelChoiceField does not override Field.validate() or Field.run_validators(). So the only validation step performed by ModelChoiceField basically checks whether the Book instance generated by Field.to_python() belongs to ModelChoiceField._get_choices() <-- which by default is a ModelChoiceIterator for all Book objects.
     7From what I can see, ModelChoiceField does not override ChoiceField.validate() or Field.run_validators(). So the only validation step performed by ModelChoiceField basically checks whether the Book instance generated by Field.to_python() belongs to ModelChoiceField._get_choices() <-- which by default is a ModelChoiceIterator for all Book objects.
    88
    99Is there a "lighter" way of doing this that simply checks whether the ModelChoiceField value (as a PK) exists in the corresponding model table? If the SELECT query is simply a side effect of ModelChoiceField inheriting Field.clean(), then I think it would be cleaner design to have ModelChoiceField.clean() override Field.clean() and avoid calling to_python() in the default validation loop. And as mentioned fetch the object only if needed for custom validators.
Back to Top