A question of signals and ATOMIC_REQUESTS

28 views
Skip to first unread message

mesulphur

unread,
Jan 31, 2018, 12:54:29 AM1/31/18
to django...@googlegroups.com
When ATOMIC_REQUESTS=True is set, are ORM signals - post_save and
pre_save - also executed within the transaction? Per my inference, they
seem be to a part of the transaction.

Assuming my inference is correct, then consider the following code


@receiver(post_save, sender=MyModel)
def log_and_notify_change(sender, instance, created, **kwargs):
if not created:
old_copy = MyModel.objects.get(id=instance.id)
diff_fn(old_copy, new_copy)


Since Postgresql only supports READ COMMITTED, implying that reading a
row inside a transaction should always return the older value. However,
in the code above, old_copy always has updated values meaning that the
signal was executed outside transaction (after it was committed.)

Is this the correct behavior? I tried to reason by looking at the code
but again not sure.

Thanks!

Jason

unread,
Jan 31, 2018, 6:11:16 AM1/31/18
to Django users
It seems that your inference is correct

https://stackoverflow.com/a/41522066/214892

Me Sulphur

unread,
Jan 31, 2018, 8:54:00 AM1/31/18
to Django users
Thanks! For the response.

In that scenario, what would explain old_copy (in the code example) having updated values and not the stale ones from the DB.

Postgres Doc Reference: https://www.postgresql.org/docs/current/static/sql-set-transaction.html
Reply all
Reply to author
Forward
0 new messages