I am extremely newbie to Django, Sorry in the advance for asking the wrong question.
I want to create a function to sum the values of a column and call it on my template in Django.
Model:
class Invoice(models.Model):
product_name = models.CharField(max_length=255)
purchase_date = models.DateTimeField(auto_now=True)
cost_price = models.IntegerField()
selling_price = models.IntegerField()
@property
def get_total(self):
amount = Invoice.objects.aggregate(Sum('selling_price'))
return amount
I created this get_total function to sum of the values of selling_price column.
Template Code:
{% for item in query_result %}
<tr>
<td>{{item.product_name}}</td>
<td>{{item.cost_price}}</td>
<td>{{item.selling_price}}</td>
{{item.get_total}}
</tr>
{% endfor %}
Here's the problem, after passing the data, whenever I call this function, the data is shown multiple times in the template.
Here's the output. Output Image
I know it's quite silly, but I am unable to figure it out.
Help, Please.