7.3.8 Understanding ModelForm Validation

One of Django’s central gotchas revolves around misunderstanding validation in forms inheriting ModelForm.

Much like a regular Django form, a ModelForm runs all of the clean and validator methods associated with the form (which is why we keep clean_name() and clean_slug()). However, a ModelForm also uses model validation.

That’s right: models also provide validation! Models supply a full_clean() method, which is similar to the form’s is_valid() method. Model validation is very similar to form validation. The model starts by validating individual fields with validators and clean methods. The model itself may supply field-specific clean methods, just as the form does, and the model may also similarly supply ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.