[Fixed]-How to use full_clean() for data validation before saving in Django 1.5 gracefully?

31👍

Even though the idea of enforcing validation on Model level seems right, Django does not do this by default for various reasons. Except for some backward-compatibility problems, the authors probably don’t want to support this because they fear this could create a false feeling of safety when in fact your data are not guaranteed to be always validated. Some ORM methods (e.g. bulk_create or update) don’t call save() and thus are unable to validate your models.
In other words, it is hard to guarantee the validation, thus they’ve decided not to pretend it.

If you need this for multiple models, you can create a simple mixin that overrides the save() method and calls full_clean() before super. Do note that this might cause the validation to be run twice in some cases, like when using ModelForm. It might not be that of an issue though if your validation routines are side-effect free and cheap to run.

For more info, please see these answers:

Leave a comment