I have a post_save handler that inserts additional records into the database referring to the instance that was just created or updated. However, an error (perhaps a constraint violation) may occur when inserting the additional records.
If an exception occurs in the post_save handler, is it still possible that the initial instance will be committed?
The answer might depend on these sub-questions:
- Does Django’s auto-commit mode commit before or after the
post_savesignal? - Does Django attempt to use nested transactions to rollback the instance being saved if an error occurs in
post_save?
According to the docs, if you are using
autocommitchanges to the initial instance will be committed on.save()before anypost_savesignal handler. An exception inpost_savewill not rollback the changes to the initial instance.You can confirm this by looking at the source to
save_baseindjango/db/models/base.py. The autocommit would occur on line 555 (in 1.4.2), but thepost_savesignal isn’t sent until line 564. You can also see that Django does not attempt to use any nested transactions in.save().If you are using
django.middleware.transaction.TransactionMiddlewareand have not overridden its behavior with anautocommitdecorator, an exception duringpost_savewould rollback the entire transaction, including the changes to the initial instance.