24👍
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)
15👍
Hope this may help someone using Django 1.9 or later. Since 1.9 on_commit is available.
So basically you would be doing it like this:
from django.db import transaction
transaction.on_commit(
lambda: send_msg_to_rabbitmqp(param1, param2, ...)
)
If you wish to keep post_save
, you can still use on_commit
:
@receiver(pre_save, sender=MyModel)
def my_handler(sender, instance, created, **kwargs):
transaction.on_commit(
lambda: send_msg_to_rabbitmqp(instance.id)
)
- [Django]-Django-Forms with json fields
- [Django]-How do I pass a PK or slug to a DetailView using RequestFactory in Django?
- [Django]-Django serve static index.html with view at '/' url
5👍
I have implemented transaction signals (post_commit
and post_rollback
) by monkey patching django:
http://gist.github.com/247844
- [Django]-Django @login_required decorator for a superuser
- [Django]-Django Facebook Connect App Recommendation
- [Django]-How to update code in a docker container?
1👍
One possibility would be to subclass the transaction middleware so that it sends a custom signal on commit. Your code could listen for that signal, rather than post_save.
- [Django]-Django rest framework serializing many to many field
- [Django]-How django time zone works with model.field's auto_now_add
- [Django]-Add a custom permission to a User
1👍
Have a look at django-celery-transactions for a solution to this.
I’ve recently finished splitting-out and refactoring the underlying signals code code into a stand-alone app django-db-signals.
- [Django]-Why would a django test fail only when the full test suite is run?
- [Django]-Create a field whose value is a calculation of other fields' values
- [Django]-Django [Errno 13] Permission denied: '/var/www/media/animals/user_uploads'