[Answered ]-How to implement transaction rollback in database if there is insertion in more than one model consecutively?

1👍

You can use transaction.atomic() for those db queries that you want to be either created all or none. I have written code snippet you can refer from here :-

from django.db import transaction

def user_signup(request):
    if request.method == 'POST':
        # Signup for student
        if(request.POST['stud_fname']):
            stud_fname = request.POST['stud_fname']
            stud_lname = request.POST['stud_lname']
            stud_uni = request.POST['stud_uni']
            stud_email = request.POST['stud_email']
            stud_password = request.POST['stud_passwd']

            try:
                with transaction.atomic():
                    user = User.objects.create_user(first_name=stud_fname, last_name=stud_lname, username=stud_email, password=stud_password)
                    StudentUser.objects.create(user=user, university=stud_uni, user_type="Student")
                messages.success(request, "Account successfully created. You can now log in")
                return redirect('login')

            except:
                messages.error(request, "Failed to create account. Try again!")
                return render(request, 'signup.html')

    else:
        return render(request, 'signup.html')

Leave a comment