Sometimes it is not possible to know beforehand and graciously reject model save inside a validator which shows nice error message in Django Admin.
If a specific crash happens during the save operation (e.g. data integrity error) and we still want to catch it and show a nice error (similar to validation errors), there's no obvious way to do this that I could find.
I tried overriding the save_model
method on Django Admin, but this is a horrible thing to do according to the docs:
When overriding ModelAdmin.save_model() and ModelAdmin.delete_model(), your code must save/delete the object. They aren’t meant for veto purposes, rather they allow you to perform extra operations.
What's the correct way to catch specific exceptions and display nice error message then?
UPDATE:
Example: integrity error when using optimistic locking.
More concrete example: ConcurrentTransition
error raised by django_fsm
when the object's state has been changed in DB since the object was loaded by Admin (this could be considered a light version of optimistic locking).