Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django: how do i substract numbers from another number and return the new_value in django

i have a model field called main_all_earning and it looks like this main_all_earning = models.IntegerField(default=0), i also have a form where user are allowed to input any amount they want to withdraw from main_all_earning, i have written the logic for it to subtract the value from the form which is called amount from the main_all_earning, but the main_all_earning does noot update. What could be the issue?

Views.py


def withdrawal_request(request):
    user = request.user
    profile = Profile.objects.get(user=user)
    main_all_earning = profile.main_all_earning
    
    if request.method == "POST":
        form = WithWithdrawalRequestForm(request.POST)
        
        if form.is_valid():
            new_form = form.save(commit=False)
            new_form.user = request.user

            if new_form.amount > main_all_earning:
                messages.warning(request, "You cannot withdraw more than your wallet balance.")
                return redirect("core:withdrawal-request")

            elif pending_payout >= main_all_earning:
                messages.warning(request, "You have reached your wallet limit")
                return redirect("core:withdrawal-request")
            
            elif new_form.amount >= main_all_earning:
                messages.warning(request, "You have reached your wallet limit")
                return redirect("core:withdrawal-request")

            else:
                new_form.save()
                main_all_earning = main_all_earning - new_form.amount
                messages.success(request, f"Withdrawal Request Is Been Processed... You would get a bank alert soon")
                return redirect("core:withdrawal-request")

    else:
        form = WithWithdrawalRequestForm(request.POST)
        context = {
            "form":form,
            "main_all_earning":main_all_earning,
            
        }
        return render(request, "core/withdrawal-request.html", context)

    context = {
            "form":form,
            "main_all_earning":main_all_earning,
            
        }
    return render(request, "core/withdrawal-request.html", context)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You set the main_all_earning variable here:

main_all_earning = main_all_earning - new_form.amount

But you don’t actually set it on the profile or save it:

profile.main_all_earning = main_all_earning - new_form.amount
profile.save()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading