[Django]-Django transaction using multi-db

7👍

The transaction.commit_manually decorator was deprecated in Django 1.6. You should switch to Django’s new transaction management.

I don’t think you need to commit transactions manually, you can nest transaction.atomic.

def my_view(request):
    try:
        with transaction.atomic(using='default'):
            with transaction.atomic(using='db_one'):
                with transaction.atomic(using='db_two'):
                    MyModel_one(...).save(using='default')
                    MyModel_two(...).save(using='db_one')
                    MyModel_three(...).save(using='db_two') # raises exception
    except IntegrityError:
        return ...
    return ...

Leave a comment