Skip to content

Instantly share code, notes, and snippets.

@craiglabenz
Last active December 27, 2015 11:59
Show Gist options
  • Save craiglabenz/7323056 to your computer and use it in GitHub Desktop.
Save craiglabenz/7323056 to your computer and use it in GitHub Desktop.
@classmethod
def my_func(cls, *args):
'''
A model function -- not a full Django View.
'''
with transaction.commit_manually(using='custom_connection'):
obj = OtherModel.objects.get(...)
try:
# do stuff
obj.save()
except:
# do other stuff
obj.save()
print 'pre-commit'
transaction.commit()
print 'post-commit'
RESULTS IN:
>>> MyClass.my_func()
>>> 'pre-commit'
>>> TransactionManagementError: This code isnt under transaction management.
** Note that the `obj.save()` line hits and is reflected by my database.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment