Django Subatomic¶
Subatomic splits Django's atomic into a suite of more specific utilities.
It offers precise control over transaction logic in Django projects
and transaction simulation in tests.
Installation¶
pip install django-subatomic
Quick comparison¶
Subatomic's utilities don't exactly map one-to-one to Django's transactions API, but the table below is broadly representative. See the footnotes for some nuance and the linked API docs for full details.
| Desired outcome | Django | Subatomic |
|---|---|---|
| Create transaction | atomic()1 |
transaction()2 |
| Create savepoint | atomic()1 |
savepoint()3 |
| Run in a transaction | atomic()1 |
transaction_required()4 |
| Fail if in a transaction | assert not connection.in_atomic_block |
@durable5 |
| Run after transaction completes | transaction.on_commit() |
run_after_commit()6 |
Further reading¶
-
Django's
atomiccreates a savepoint or a transaction depending on two factors:- The arguments passed to it (
durable=andsavepoint=). - If a database transaction is already open.
For more info, see Django's atomic. ↩↩↩
- The arguments passed to it (
-
Unlike
atomic, which will create a savepoint if a transaction is already open,transactionensures the database is not already in a transaction. ↩ -
Unlike
atomic, which may create a transaction,savepointensures the database has an active transaction. ↩ -
This ensures that some code is atomic by requiring that a transaction is already open. Unlike
atomic, this never creates a transaction or a savepoint. ↩ -
This ensures that code may choose to manage its own transactions by requiring that a transaction is not already open.
Note: This shouldn't be confused with
atomic(durable=True); this never creates a transaction. ↩ -
Unlike
transaction.on_commit(), this prevents misleading code by raising an error if there is no transaction open. ↩