Upgrading to Django 1.6 has introduced a tricky problem into my code: database operations that are triggered by a post_save signal are getting contained within the with transaction.atomic(): block of Django's get_or_create().

The impact for me is that custom sql (unmanaged) isn't getting committed to the db in time.

Is there a different signal that I can use that gets sent after obj.save() exits its atomic block?

Or have I diagnosed this wrong?

有帮助吗?

解决方案

Django's get_or_create() executes its save() in an atomic block, and signals fired post_save are nested within that atomic block.

My workaround was to override the native get_or_create() with my own version in a custom manager, without the transaction.atomic() block.

其他提示

for the signal to function properly you need out of the atomic transaction, because in the midst of shooting transsaccion signal without the commmit in the database.

Old question, but I came across this today.

Django >=1.9 supports transaction.on_commit hooks.

Here's how my resulting code came out, and appears to behave as expected:

from django.db import transaction

def my_post_save_hook(sender, instance):
    transaction.on_commit(
        lambda: do_the_thing()
    )

post_save.connect(my_post_save_hook, sender=MyModel)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top