Django ATOMIC_REQUESTS Operations

I really don't understand how atomic queries are defined in Django. If the ATOMIC_REQUESTS parameter is set to True in the DB settings, does it mean that all views are now executed in a transaction? What if I want only certain views to be executed in a transaction? Do I then have to explicitly define all the others that are not executed in the transaction using the decorator @transaction.non_atomic_requests

?

+3


source to share


1 answer


If ATOMIC_REQUESTS is set to True in the DB settings, does that mean all views are now executed in a transaction?

Yes. From the docs :

Before calling the view function, Django starts a transaction. If the response is generated without issue, Django commits the transaction. If the view throws an exception, Django rolls back the transaction.

Do I need to explicitly define all the others that are not executed in the transaction using the @transaction.non_atomic_requests

decorator?



Yes.

When ATOMIC_REQUESTS

enabled, it can still prevent views from running in a transaction. Decorator non_atomic_requests

] will negate the effect ATOMIC_REQUESTS

for this view.

After you decide on a case-by-case basis to use transactions, I prefer not to use ATOMIC_REQUESTS

and just use transaction.atomic

(be it a decorator or a context manager) where necessary. Here's an example from the documentation :

@transaction.atomic
def viewfunc(request):
    # This code executes inside a transaction.
    do_stuff()

      

+7


source







All Articles