Using Django to summarize Report

≡放荡痞女 提交于 2019-12-31 03:12:21

问题


I'm trying to produce a table that will show the total maturity amounts for each financial institution that a plan has.

A plan is the term I use for person. So each person can have multiple investments. I'm having trouble creating a table that will do this correctly.

Currently I have a report that displays each investment sorted by Maturity Date, i.e. 2011,2012, etc.

At the bottom I would like to place this summary table, but my query displays duplicates of each financial institution, due to being able to have multiple investments.

My current query:

list = Investment.objects.all().filter(plan = plan).order_by('financial_institution').filter(maturity_date__gte= '%s-1-1' % current_year).distinct()

This will output:

  • TD Bank $10k
  • TD Bank $10k
  • Scotia Bank $10k
  • Etc

But I'd like:

  • TD Bank $20k
  • Scotia Bank $10k
  • Etc

回答1:


So, you want to aggregate the values of the investments for a plan:

from django.db.models import Sum
my_plan.investment_set.filter(
    maturity_date__gte=whenever
).values('financial_institution').annotate(Sum('value'))


来源:https://stackoverflow.com/questions/7600431/using-django-to-summarize-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!