znawca
znawca

Reputation: 279

How to do math calculations in django?

I've created shop. I have two models:

class Product(models.Model):
    name = models.CharField(verbose_name="name", max_length=40)
    cost = models.FloatField(verbose_name="price")

    def __unicode__(self):
        return self.name

class Shop(models.Model):
    product = models.ManyToManyField(Product)
    name = models.CharField(verbose_name="Nazwa", max_length=40)
    budget = models.FloatField(verbose_name="kwota")

    def __unicode__(self):
        return self.name

I created template and now I have name of shop and products with their price:

https://i.sstatic.net/dGimm.png

How can I count this prices? For example like on this picture i choose products which count total = 17. Should I create something in view and next put it into template or write it only in template?

Next i want compare total price of products with budget to check which is bigger. Must i create if statement?

Upvotes: 0

Views: 2450

Answers (2)

Paul Tuckett
Paul Tuckett

Reputation: 1073

Add a calculate_prices method to your model. Then, I would change the product M2M to a FK(if you want it to be a M2M then you'd access the product model differently). Then do an if statement inside your method. Then add shop.calculate_prices to your template.

Add the method to your model:

class Shop(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="products")
    name = models.CharField(verbose_name="Nazwa", max_length=40)
    budget = models.FloatField(verbose_name="kwota")

    def __unicode__(self):
        return self.name

    def calculate_prices(self):
        all_costs = self.product.products.all()
        price_list = [x.cost for x in all_costs]
        add_price = sum(price_list)
        if add_price >= self.budget:
            return "You're in danger of overspending my friend"
        elif add_price <= self.budget:
            calc_budget = self.budget - add_price
            return "You are under budget with ${} left over".format(calc_budget) 
        else:    
            return "Cannot Compute, please add in a budget and/or product prices." 

Add in a view:

from django.http import HttpResponse
from app_directory.models import Shop
from django.template import loader


def index(request):
    shop_item = Shop.objects.first()
    shop_calc = shop_item.calculate_prices()
    template = loader.get_template("template_directory/index.html")
    context = {
        "shop_calc": shop_calc,
    }
    return HttpResponse(template.render(context, request))    

Add the template tag into your index.html:

{% if shop_calc %}
    {{shop_calc}}
{% endif %}

Upvotes: 1

Magda
Magda

Reputation: 1248

I would add a function in the view that returns the template, because you might get problems with more complicated calculations if you calculate them in the template (also this might get messy).

Yes, you could work with an if-statement in the template.

Upvotes: 1

Related Questions