Home > front end >  Django Product price calculation
Django Product price calculation

Time:09-19

Here I am having a product detail.

class product(models.Model):

    Product_name = models.CharField(max_length=50, null=True)
    product_description = models.TextField(max_length=170, null=True)
    product_price = models.SmallIntegerField(null=True)

    def __str__(self):
        return self.Product_name

Depending on the product selection the value "30" has to change dynamically pending on the product. So that It will calculate price automatically. Please help me.

if form.is_valid():
    user = form.save(commit=False)
    products = form.cleaned_data['Product']
    Quantity = form.cleaned_data['Quantity']

    shoping_product = product.objects.filter(Product_name= products)
    sub_total = 30 * Quantity
    user.Gst = 30 * (1   0.18)
    user.Price = sub_total   user.Gst

    user.save()
    messages.success(request, 'Registration successful.')
    return redirect('home')

CodePudding user response:

30 is the unit price of the product ?

shoping_product = product.objects.filter(Product_name= products).first()
sub_total = shoping_product.product_price * Quantity
user.Gst = shoping_product.product_price * (1   0.18)
user.Price = sub_total   user.Gst
  • Related