Created
April 28, 2021 10:41
-
-
Save vivekrk1992/8ac6acef47a5ff8254018208ab4a243c to your computer and use it in GitHub Desktop.
Django DB Rollback
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
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