I made the following changes in my views.py
#Imported the references from django.db import transaction class Project_Create(generics.CreateAPIView): #Added decorator @transaction.atomic def create(self, request, *args, **kwargs): try: project_instance = Project.objects.create( planner=loggedin_user, project_name=project_name) project_instance.creator_id = user.id project_instance.save() some_condition = True if some_condition: # Return, rolling back transaction when atomic block exits transaction.set_rollback(True) return except Exception as e: raise
All I want to do is, I want to roll back the transaction if there is an error. I want to delete the created project from MongoDB if there is an error. Am I missing anything? Are there any changes in a model? or settings.py? or any other file?
In fact, I want to undo all the changes made in the function before the error ocurres.