Updating or saving a form field using cleaned_data in Django not working
It is simple, but I don't know why it is not working! I just want to set my TOTAL field from the form with the PRE-TOTAL value. It is updating my FORM_DATA (instance using cleaned_data), but not saving! It is still coming with the default value!!! Would someone tell me why?
Here go the codes:
MODELS.PY
from django.db import models
from django.utils import timezone
class av_pomades(models.Model):
cod_prod = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
price = models.FloatField(default=0)
pic = models.ImageField(default='default.jpg', blank=True)
def __str__(self):
return self.name
pomades_to_chose = (
('Suavecito - water based','Suavecito - water based'),
('Reuzel Blue - water based','Reuzel Blue - water based'),
('Layrite - water based','Layrite - water based'),
('Suavecito - oil based','Suavecito - oil based'),
('Reuzel Pink - Oil Based','Reuzel Pink - Oil Based'),
('Layrite - oil based','Layrite - oil based')
)
class order_form(models.Model):
pomade = models.CharField(max_length=100, choices=pomades_to_chose)
amount = models.PositiveIntegerField()
ord_date = models.DateField(default=timezone.now)
total = models.FloatField(default=0)
def __str__(self):
return self.pomade
FORMS.PY
from dataclasses import fields
from tabnanny import verbose
from django import forms
from . import models
class order_forms(forms.ModelForm):
class Meta:
# model that the fields will come from
model = models.order_form
# chosing the fields to be displayed
fields = ['pomade', 'amount', 'total']
VIEWS.PY
from django.shortcuts import render, redirect
from .models import av_pomades, order_form
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from . import forms
@login_required(login_url='accounts:login')
def purchase(request):
if request.method == 'POST':
form = forms.order_forms(request.POST)
if form.is_valid():
# creating a cleanned data instace
form_data = form.cleaned_data
ch_pomade = str(form_data['pomade'])
# retrieving the right price from the selected product
pomades_prices = av_pomades.objects.all().values('name','price')
for pomades_data in pomades_prices:
if ch_pomade in pomades_data['name']:
ch_pomade_price = pomades_data['price']
# setting the total price
famount = int(form_data['amount'])
pre_total = ch_pomade_price * famount
# updating the price in the for to be recorded in DB
**# HERE IS THE PROBLEMATIC PART I GUESS**
form_data['total'] = pre_total
print(form_data)
form.save()
return redirect('products:order_success')
else:
form = forms.order_forms()
return render(request,'basquet.html', {'order_form':form})
Go through the answers in this question How do I pass a variable by reference? to understand the specifics of what you were facing.
In your case, you are not updating the form data but a variable that is an independent copy of the form data
WRONG
form_data['total'] = pre_total
CORRECT
form.cleaned_data['total'] = pre_total