On the model page, I created a property called nzd_price
, which is not really a field, but a property. On a templatetags page, I tried to aggregate the sum all the rows of a column from nzd_price
for a new row with the name Total
:
models.py
:
class BrazilBill(models.Model):
item = models.CharField('item', max_length = 50)
price = models.DecimalField('price', max_digits = 10, decimal_places = 2)
@property
def nzd_price(self):
return round(float(self.price) * 0.29, 2)
templatetags/total_price.py
:
from django import template
from django.db.models import Sum
from financial_planning.models import BrazilBill
register = template.Library()
@register.filter
def brazil_bill_total_brl(value):
return BrazilBill.objects.aggregate(Sum('price')).get('price__sum')
@register.filter
def brazil_bill_total_nzd(value):
return BrazilBill.objects.aggregate(Sum('nzd_price')).get('nzd_price__sum')
The error:
FieldError at /financial-planning/
Cannot resolve keyword 'nzd_price' into field. Choices are: id, item, price
Based on Calculate the sum of model properties in Django, I also tried:
@register.filter
def brazil_bill_total_nzd(value):
return BrazilBill.objects.aggregate(Sum('brazilbill__nzd_price'))