Skip to content

Instantly share code, notes, and snippets.

@stuaxo
Last active January 12, 2021 17:41
Show Gist options
  • Save stuaxo/4eca59083b005a6cb58f7451be706918 to your computer and use it in GitHub Desktop.
Save stuaxo/4eca59083b005a6cb58f7451be706918 to your computer and use it in GitHub Desktop.
Aborting transaction context manager.
"""
Aborting transaction context manager, useful for when prototyping on Django.
"""
from contextlib import contextmanager
from django.db import transaction
class AbortTransaction(Exception):
pass
@contextmanager
def aborting_transaction():
try:
with transaction.atomic():
yield
raise AbortTransaction()
except AbortTransaction:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment