So I'm making an expense sheet django app and I'm stuck trying to render the Sum of all the inputted expenses.
I've created a custom manager to calculate the sum:
class ExpenseManager(models.Manager):
def price_sum(self):
return super(ExpenseManager, self).aggregate(total_price = Sum('price'))['total_price']
And added it to my model:
class Expense(models.Model):
...
objects = models.Manager()
price_object = ExpenseManager()
...
I know my manager works because when I execute it in the shell I get the correct sum of my expenses -- i.e. I put in Expense.price_object.price_sum()
and I get back Decimal('254.77')
-- but when I try to get that into my template it just shows up blank.
I've tried putting in my variable a couple different ways but none of it has worked, for example:
{{price_object.price_sum}}
or
{{expense.price_object.price_sum}}
or me getting desperate...
{% for p in expense.price_object %} {{p.price_sum}} {% endfor %}
or
{% for p in expense.price_object.price_sum %} {{p}} {% endfor %}
but yeah... nothing ever shows up when I load the page. Can anyone help?