I'm not sure how to properly raise a validation error in a model's save method and send back a clear message to the user.
Basically I want to know how each part of the "if" should end, the one where I want to raise the error and the one where it actually saves:
def save(self, *args, **kwargs):
if not good_enough_to_be_saved:
raise ValidationError
else:
super(Model, self).save(*args, **kwargs)
Then I want to know what to do to send a validation error that says exactly to the user what's wrong just like the one Django automatically returns if for example a value is not unique. I'm using a (ModelForm) and tune everything from the model.
be sure to import the ValidationError as well
You can just stick a
clean
method to your model most of the time, but you don't have that option necessarily with the built inauth.User
model. This solution will allow you to create aclean
method for theauth.User
model in such a way thatValidationError
s will propagate to forms where the clean method is called (including admin forms).The below example raises an error if someone attempts to create or edit an
auth.User
instance to have the same email address as an existingauth.User
instance. Disclaimer, if you are exposing a registration form to new users, you do not want your validation error to call out usernames as mine does below.I have this at the bottom of
my_app.models
but I am sure it would work as long as you stick it somewhere that is loaded before the form in question.Most Django views e.g. the Django admin will not be able to handle a validation error in the save method, so your users will get 500 errors.
You should do validation on the model form or on the model, and raise
ValidationError
there. Then callsave()
only if the model form data is 'good enough to save'.Bastian, I explain to you my code templating, I hope that helps to you:
Since django 1.2 it is able to write validation code on model. When we work with modelforms, instance.full_clean() is called on form validation.
In each model I overwrite
clean()
method with a custom function (this method is automatically called from full_clean() on modelform validation ):Then in
rules.py
file I write bussiness rules. Also I connectpre_save()
to my custom function to prevent save a model with wrong state:from issues.models import Issue
Then, modelform calls model's clean method and my custon function check for a right state or raise a error that is handled by model form.
In order to show errors on form, you should include this on form template:
The reason is that model validation erros ara binded to non_field_errors error dictionary entry.
When you save or delete a model out of a form you should remember that a error may be raised:
Also, you can add errors to a form dictionary on no modelforms:
Remember that this code is not execute on save() method: Note that full_clean() will not be called automatically when you call your model’s save() method, nor as a result of ModelForm validation. Then, you can add errors to a form dictionary on no modelforms: