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)
CodePudding user response:
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()
CodePudding user response:
You need to save it on the profile like this
## chain the main_all_earning to the profile object
profile.main_all_earning = main_all_earning - new_form.amount
## Save the new value
profile.save()