[Fixed]-Confusion about transaction.atomic() in Django

1👍

If you are using multiple databases, you should specify which database to use with using:

with transaction.atomic(using='my_db'):
    modelA.save()
    modelB.save() 

0👍

I found a solution:

transaction.set_autocommit(False, using='my_db')
modelA.save()
modelB.save()
transaction.commit(using='my_db')
transaction.set_autocommit(True, using='my_db')

My issue was that a constraint on the db table in question meant the saves had to be done as one transaction. For some reason transaction.atomic() didn’t work…

Leave a comment