on_commit(func, using=None)
[source]
Pass any function (that takes no arguments) to on_commit()
:
from django.db import transaction def do_something(): pass # send a mail, invalidate a cache, fire off a Celery task, etc. transaction.on_commit(do_something)
You can also wrap your function in a lambda:
transaction.on_commit(lambda: some_celery_task.delay('arg1'))
The function you pass in will be called immediately after a hypothetical database write made where on_commit()
is called would be successfully committed.
If you call on_commit()
while there isn’t an active transaction, the callback will be executed immediately.
If that hypothetical database write is instead rolled back (typically when an unhandled exception is raised in an atomic()
block), your function will be discarded and never called.
Please login to continue.