Database Transactions

Transaction and Commit

Djongo integrates with MongoDB Transactions API to support multi document atomic transactions. Atomic transactions are enabled in Django using the usual transaction.atomic() decorator or context manager. MongoDB transactions significantly speed up Django test execution and validation.

Example

from djongo import transaction

def viewfunc(request):
    stuff()

    with transaction.atomic():
        # This code executes inside a transaction.
        more_stuff()
copy code

This produces the following pymongo commands:

session = cli.start_session()
transaction = session.start_transaction()
# more_stuff
transaction.commit_transaction() # or transaction.abort_transaction()
session.end_session()
copy code