how to create a table with sum of two different queryset

Viewed 36

I have the following model in my django app:

class Budget_Vendite(models.Model):
    product=models.ForeignKey()
    byproduct=models.ForeignKey()
    quant_jan=models.DecimalField()
    quant_feb=models.DecimalField()
    quant_mar=models.DecimalField()
    price_jan=models.DecimalField()
    price_feb=models.DecimalField()
    price_mar=models.DecimalField()

I want to create a dataset that for each byproduct figure out the montly amount given by quant_jan*price_jan Ad example:

byproduct   |         jan         |         feb         |            
byproduct_1 | quant_jan*price_jan | quant_feb*price_feb |
1 Answers

Define methods in your model, each for each month, example:

def january(self):
    return self.quant_jan * self.price_jan
.
.
.

def march(self):
    return self.quant_mar * self.price_mar

and call them in your template html in corresponding column

monthly sum:

def year_sum(self):
    months = ((quant_jan, price_jan), ...)
    sum = 0
    for month in months:
        sum += getattr(self, month[0]) * getattr(self, month[1])
    return sum
Related