Created
April 15, 2014 21:21
-
-
Save colorstain/10776676 to your computer and use it in GitHub Desktop.
autocommit off decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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