dtc
dtc

Reputation: 1849

Django: does transaction atomicity ensure that code afterwards happens after the database is saved?

Hope the question is clear. I'm trying to handle saving synchronously after model.save() but I can't use django signals for certain reasons (So please don't mention that as the possible solution)

I would have :

def viewfunc(request):
    # This code executes in autocommit mode (Django's default).
    do_stuff()

    with transaction.atomic():
        # This code executes inside a transaction.
        do_more_stuff()
    do_even_more_stuff()

When I run do_even_more_stuff(), is it safe to assume that any model.save() done with do_more_stuff() has already been saved to the database?

Upvotes: 2

Views: 262

Answers (1)

Paulo Scardine
Paulo Scardine

Reputation: 77399

According to the docs:

if the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.

So the answer for the general case is no, but it is safe to assume this if the block completes without exceptions.

Upvotes: 3

Related Questions