While subclassing db.models.Model, sometimes it's essential to add extra checks/constraints. E.g. I have an Event model with start_date and end_date. I want to add validation into the fields or the model so that end_date > start_date. How many possible ways to do this? At least I know this can be done outside the models.Model inside the ModelForm validation. But how to attach to the fields and the models.Model?
+3
A:
I would not put constraints like these in the save method, it's to late. Raising an exception there, doesn't help the user who entered the data in the wrong way, because it will end up as a 500 and the user won't get the form with errors back etc.
You should really check for this in the Forms/ModelForms clean method and raise a ValidationError, so form.is_valid()
returns false and you can send the errors in the form back to the user for correction.
Also note that the Django Development version has Model Validation.
stefanw
2010-02-17 14:32:12