Skip to content

Instantly share code, notes, and snippets.

@vivekrk1992
Created April 28, 2021 10:41
Show Gist options
  • Save vivekrk1992/8ac6acef47a5ff8254018208ab4a243c to your computer and use it in GitHub Desktop.
Save vivekrk1992/8ac6acef47a5ff8254018208ab4a243c to your computer and use it in GitHub Desktop.
Django DB Rollback
first import the tracsaction contrl
```
from django.db import transaction
```
then code like this
```
@api_view(['POST'])
@transaction.atomic
def signup(request):
sid = transaction.savepoint()
try:
...
...
transaction.savepoint_commit(sid)
except Exception as e:
transaction.rollback(sid)
```
step 1: import the library
step 2: use transaction decorator
step 3: create save point id (sid) so we can roll back or commit for particular stage
step 4: if function run successfully then the transaction will commited
step 5: otherwise the rollback operaion revert the database changes till sid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment