Showing custom model validation exceptions in the Django admin site.

Posted by Guy Bowden on Stack Overflow See other posts from Stack Overflow or by Guy Bowden
Published on 2010-02-01T15:25:18Z Indexed on 2011/01/05 3:53 UTC
Read the original article Hit count: 222

Filed under:
|

I have a booking model that needs to check if the item being booked out is available. I would like to have the logic behind figuring out if the item is available centralised so that no matter where I save the instance this code validates that it can be saved.

At the moment I have this code in a custom save function of my model class:

def save(self):
    if self.is_available(): # my custom check availability function
        super(MyObj, self).save()
    else:
        # this is the bit I'm stuck with..
        raise forms.ValidationError('Item already booked for those dates')

This works fine - the error is raised if the item is unavailable, and my item is not saved. I can capture the exception from my front end form code, but what about the Django admin site? How can I get my exception to be displayed like any other validation error in the admin site?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-admin