Skip to content

Instantly share code, notes, and snippets.

@colorstain
Created April 15, 2014 21:21
Show Gist options
  • Save colorstain/10776676 to your computer and use it in GitHub Desktop.
Save colorstain/10776676 to your computer and use it in GitHub Desktop.
autocommit off decorator
from functools import wraps
from django.db import transaction
def autocommit_off(func):
"""
Decorator to turn autocommit off inside a function, function should commit or rollback before returning.
:param func: Function to be decorated
:return: decorated function
"""
@wraps(func)
def inner(*args, **kwargs):
transaction.set_autocommit(False)
try:
return func(*args, **kwargs)
finally:
transaction.set_autocommit(True)
return inner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment