10๐
I think you should be able to achieve that by doing this:
from django.db.models import F, Sum
(ModelA.objects.all()
.prefetch_related('modelb_set', 'modelb__modelc_set')\
.values('name')\ # group values by modela.name, read: https://docs.djangoproject.com/en/1.9/topics/db/aggregation/
.annotate(name = F('name'),
total_amount = Sum('modelb__modelc__amount')))
and in your template you should use:
{% for modela in modela_list %}
{{ modela.name }}, {{ modela.total_amount }}
{% endfor %}
๐คMiao ZhiCheng
0๐
You can simplify this down to:
from django.db.models import F, Sum
ModelA.objects.all()
.prefetch_related('modelb__modelc_set') \
.values('name') \
.annotate(name=F('name'), total_amount=Sum('modelb__modelc__amount'))
I know this works in later versions of Django.
๐คnatehawkboss
- How to display a user's get_full_name() instead of the username in a Django model form?
- Django Storages โ Could Not Load Amazon's S3 Bindings Errors
- Args and kwargs in django views
0๐
i did it by using django internals to check if related queryset is evaluated / prefetched
from django.db.models import Sum, QuerySet, Model
def is_evaluated_queryset(qs):
return isinstance(qs, QuerySet) and qs._result_cache is not None
class Order(Model):
"""Order can have multiple payments"""
def get_total_payment(self):
""" returns sum of all the payments for this order"""
payments_qs = self.payment_set.all()
if is_evaluated_queryset(payments_qs):
# qs is prefetched - better not to use aggregate
return sum(payment.amount for payment in payments_qs)
else:
return payments_qs.aggregate(total=Sum('amount'))['total'] or 0
๐คpymen
- Setting HTTP_REFERER header in Django test
- PIL โ libjpeg.so.8: cannot open shared object file: No such file or directory
- Django collectstatic no such file or directory
Source:stackexchange.com