Is there any way to run some code after transaction commit in Django?
I need to send some messages to a rabbitmq server for offline processing, but the message gets to the consumer before the Django transaction is commited.
My message is sent in the post_save signal of the model. What I'm looking for is a similar mechanism, using signals or something else, that would execute code after the commit (and do nothing if the transaction fails).
I haven't found any generic way of doing it in Django. Do you have any ideas?
Django provides the on_commit() function to register callback functions that should be executed after a transaction is successfully committed: on_commit (func, using=None)[source]
Django's default behavior is to run in autocommit mode. Each query is immediately committed to the database, unless a transaction is active. This means if in a view function there are 2 delete operation and one update then will run one by one and if the last one failed the two fist operation is done to database.
You can't execute queries until the end of the 'atomic' block." is raised when you try to used a database connection after a database exception even those autocommit was set to false from the start. It should be up to the user how to handle the database exception and the transaction as autocommit was set to false.
In terms of Django, optimistic concurrency control can be implemented by overriding the save method on your model class... And, of course, for either of these concurrency mechanisms to be robust, you have to consider transactional control.
django-transaction-hooks solves this problem for Django < 1.9, and the functionality is built into Django 1.9+:
from django.db import transaction def do_something(): pass # send a mail, invalidate a cache, fire off a Celery task, etc. transaction.on_commit(do_something)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With