Skip to content
Advertisement

Catch IntegrityError in Django Admin

Working with Django 1.4

I would like to catch an Integrity Error when a user does enter a duplicate value. At this moment the application just shows the default Django error page. (debug = True)

model

JavaScript

admin

JavaScript

form

JavaScript

At this moment I get an error page showing a ValidationError instead of an IntegrityError. (Which makes perfect sense)

How would I be able to properly catch the IntegrityError and show it at the top of the Django admin page?

Update

I noticed there was a form in there to check if foo.parent.bar.identifier matches foo.bar.identifier.
When I remove the form from the ViewAdmin the error handling works as expected.

So I guess the question here would be: How do I check if the parents match when saving the admin form?

Advertisement

Answer

You shouldn’t try to handle this in the save_model method. The admin view expects the save_model method to succeed, so does not handle integrity or validation errors. If the save_model method is raising IntegrityError, then the problem is probably somewhere else.

I think your problem is that you have forgotten to call the parent class’ clean method when you overrode it. This prevents the form from validating the unique field. See the warning in the docs for more info.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement