Trigering post_save signal only after transaction has completed

前端 未结 3 734
旧巷少年郎
旧巷少年郎 2020-12-30 02:27

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

3条回答
  •  孤城傲影
    2020-12-30 03:26

    I was having serious issues with django's admin not allowing post_save transactions on parent objects when they had inline children being modified.

    This was my solution to an error complaining about conducting queries in the middle of an atomic block:

    def on_user_post_save_impl(user):
         do_something_to_the_user(user)
    
    def on_user_post_save(sender, instance, **kwargs):
        if not transaction.get_connection().in_atomic_block:
            on_user_post_save_impl(instance)
        else:
            transaction.on_commit(lambda: on_user_post_save_impl(instance))
    

提交回复
热议问题