from django.db import transaction @transaction.atomic def viewfunc(request): # This code executes inside a transaction. do_stuff()