Django post_save preventing recursion without overriding model save()

前端 未结 9 1598
轻奢々
轻奢々 2020-11-29 23:41

There are many Stack Overflow posts about recursion using the post_save signal, to which the comments and answers are overwhelmingly: \"why not override save()\

9条回答
  •  广开言路
    2020-11-30 00:24

    How about disconnecting then reconnecting the signal within your post_save function:

    def my_post_save_handler(sender, instance, **kwargs):
        post_save.disconnect(my_post_save_handler, sender=sender)
        instance.do_stuff()
        instance.save()
        post_save.connect(my_post_save_handler, sender=sender)
    post_save.connect(my_post_save_handler, sender=Order)
    

提交回复
热议问题