Confused regarding model versus form validation

Hi. My first question here - hope it’s not too naïve.

I have been doing what people so often here warn against doing - trying to build and maintain an app using a heavily customized admin as the front end. I’ve finally accepted that I’m way past the point of diminishing returns, and am in the process of replacing my most heavily customized model admins with custom views.

One thing I’m somewhat confused about is the roles of data validation in forms versus in models. I currently have some quite complex business logic implemented in the full_clean() methods of my models. Since I’ll now be using custom views and forms for these models, is there any compelling reason why I should consider moving my validation logic from the models to the forms, or splitting it between the two?

If the custom forms used are model forms, then the models’ full_clean() methods will be called during form validation (review docs on validation of model forms).

As to the roles of validation in forms vs models:

Form validation is useful for validating user input before saving the model; validating form-specific constraints (e.g matching password1 and password2) and providing user-friendly error messages.

Model validation mostly for ensuring data integrity before saving to the database.

1 Like