run code after transaction commit in Django

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 17:57:46

UPDATE 2: django-transaction-hooks was merged into Django core and released in Django version 1.9.

UPDATE: django-transaction-hooks solves this problem.

I don't believe there is a clean way to do this; at least I can't think of one. You could monkeypatch django.db.transaction.commit to send a custom signal; not pretty but I think it would work.

Might also be interesting to raise this use-case on the django-developers mailing list. The devs are generally averse to adding new signals, but you might have a good case here (and a rebuttal from a core dev might include a useful suggestion of how to resolve your situation). You're more likely to get responses if you wait until after 1.1 comes out, though.

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)
    )

I have implemented transaction signals (post_commit and post_rollback) by monkey patching django: http://gist.github.com/247844

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!