Search code examples
djangomodel-view-controllerviewmodelbusiness-logic

How to return an error message from the Model?


In suppliment to this question, if business logic should be in the model, how do I return an error message from the model?

def save(self, *args, **kwargs):
    if <some condition>:
        #return some error message to the view or template

Solution

  • Pastylegs is correct, but you shouldn't be doing that sort of logic in the save method. Django has a built-in system for validating model instances before saving - you should use this, and raise ValidationError where necessary.