I have written some APIs, for which the respective functions executive inside a transaction block. I am calling the save()
method (after some modifications) on instance/s of a/several Model/s, and also consecutively indexing some JSON related information of the instance/s in Elasticsearch. I want the database to rollback even if for some reason the save()
for one of the instances or indexing to the Elasticsearch fails.
Now, the problem is arising that even inside the transaction block, the post_save()
signals gets called, and that is an issue because some notifications are being triggered from those signals.
Is there a way to trigger post_save()
signals only after the transactions have completed successful?
Not really. The signals have nothing to do with the db transaction success or failure, but with the save method itself - before the call you have the pre_save signal fired and after the call you have the post_save signal fired.
There are 2 approaches here:
instance.saved_successfully = True
, which you will test in the post_save handler.Makes sense?
P.S.
If you strictly need to bind to the transaction commit signal, have a look over this package: https://django-transaction-hooks.readthedocs.org/en/latest/; it looks like the functionality is integrated in Django 1.9a.