I am refactoring some legacy code on a Python/Django application and I'm looking for a styling/best practices recommendation about performing actions on a SQL Database.
Right now I am trying to rework the following code:
def fun():
try:
with transaction.atomic():
# query DB
# do business logic
# update DB entries with new values
except:
# handle exception
The thing is, there is a fair amount of business logic between my query and my update. However I feel like it doesn't make sense to throw additional code under another indentation/i.e defining as part of a DB transaction. Ideally I'd like to clean this up and only perform certain things as atomic. I suppose it wouldn't actually cut down on my lines of code, but I'm curious what transaction.atomic() is actually accomplishing?
From my understanding of atomicity, reads don't actually affect the state of a datastore, so I'm not entirely sure the initial read belongs under the atomic() anyway.
thanks in advance.