Django Transaction Hooks
Django library for running callbacks after database transactions commit successfully. Merged into Django 1.9+ as built-in feature. Supports PostgreSQL MySQL SQLite. Useful for Celery tasks emails cache invalidation. Open source free.
What it is
django-transaction-hooks is a Django library that provides a reliable way to execute callbacks after database transactions commit. It solves the problem of triggering actions like Celery tasks email notifications or cache invalidation only when a transaction succeeds. The library handles savepoints connection drops and database idiosyncrasies that make manual implementations error-prone. It was merged into Django 1.9 as a built-in feature so this third-party package is only needed for Django 1.6 through 1.8.
The library works by providing custom database backends for SQLite PostgreSQL MySQL and PostGIS. Developers replace their standard database engine with the transaction_hooks equivalent. A mixin is also available for unsupported backends. Usage is simple: pass any no-argument function to connection.on_commit and it runs after commit. If the transaction rolls back the callback is discarded. Callbacks registered outside transactions execute immediately.
Key features include ordered execution of hooks exception handling that stops later hooks and compatibility with Django autocommit mode and transaction.atomic. The library warns it is not battle-tested and may have bugs. It requires autocommit mode and does not work with lower-level transaction APIs. No rollback hook is provided by design.
This tool helps Django developers building applications that need reliable post-commit actions. It is especially useful for asynchronous task queues email systems and caching layers. The documentation covers installation setup usage patterns and important caveats. Since Django 1.9 includes this functionality natively the library serves legacy projects.
The project is open source and free. It is hosted on PyPI and documented on Read the Docs. Contributions are welcome. The codebase is relatively small focused on solving one specific problem well.
This description was generated by AI from the site's public content and may contain errors.
What you can do there
- Documentation for django-transaction-hooks library
- Installation and setup instructions for supported databases
- Usage examples with connection.on_commit
- Guidance on transaction hooks best practices
- Information on contributing to the project
Who it's for
Django developers maintaining legacy projects on Django 1.6 to 1.8 who need reliable post-commit callbacks.
Frequently asked questions about Welcome to django-transaction-hooks! — django-transaction-hooks 0.2.1.dev1 documentation
›What does django-transaction-hooks do?
It lets you register functions that run only after a database transaction commits successfully. If the transaction rolls back the functions are never called.
›Which Django versions are supported?
Django 1.6.x through 1.8.x. Django 1.9 and later include this functionality as a built-in feature so the library is not needed for newer versions.
›Which databases are supported?
SQLite3 PostgreSQL PostGIS and MySQL have built-in support. Other databases can work using the provided mixin to create a custom backend.
›How do I install and set it up?
Install with pip install django-transaction-hooks then change your DATABASES ENGINE setting to use the transaction_hooks backend for your database.
›Can I use it with Celery tasks?
Yes. A common pattern is connection.on_commit(lambda: some_celery_task.delay('arg')) to queue tasks only after commit.
›What happens if a callback raises an exception?
If an on-commit hook raises an uncaught exception no later hooks in that transaction will run. This matches standard sequential execution behavior.
›Does it work with manual transaction management?
No. It only works correctly in autocommit mode with transaction.atomic or ATOMIC_REQUESTS. Lower-level transaction APIs are not supported.
