You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Chapter or Appendix: 11.1: Validate All Incoming Data With Django Forms
Description
While the book already explains well how to validate models with ModelForm class in this section, some may prefer using validation at a model level (with validators in field declaration and/or overriding clean/clean_field methods in model).
To my view, setting validation at this lower level can decrease the risk of inserting bad data in database, especially when a project has many sources of input data, because you could forget to call the validation process.
In the case of you choose model-level validation, it could be well to warn people about the strange Django behaviour in which a call to the save method does not trigger the model validation, because the full_clean method is actually not called, for backward-compatibility purposes. I personally find this behaviour very counter-intuitive.
Location within the Book
Description
While the book already explains well how to validate models with
ModelForm
class in this section, some may prefer using validation at a model level (with validators in field declaration and/or overridingclean
/clean_field
methods in model).To my view, setting validation at this lower level can decrease the risk of inserting bad data in database, especially when a project has many sources of input data, because you could forget to call the validation process.
In the case of you choose model-level validation, it could be well to warn people about the strange Django behaviour in which a call to the
save
method does not trigger the model validation, because thefull_clean
method is actually not called, for backward-compatibility purposes. I personally find this behaviour very counter-intuitive.For this purpose, the mixin bellow is often used:
The problem and different solutions are well explained here: https://www.xormedia.com/django-model-validation-on-save/
The text was updated successfully, but these errors were encountered: