使用Django总结Report [英] Using Django to summarize Report
问题描述
我正在尝试制作一个表格,以显示计划拥有的每个金融机构的总到期金额.
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.
目前我有一份报告,显示按到期日排序的每项投资,即 2011、2012 等
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.
我当前的查询:
list = Investment.objects.all().filter(plan = plan).order_by('financial_institution').filter(maturity_date__gte= '%s-1-1' % current_year).distinct()
这将输出:
- 道明银行 1 万美元
- 道明银行 1 万美元
- 丰业银行 1 万美元
- 等
但我想:
- 道明银行 2 万美元
- 丰业银行 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'))
这篇关于使用Django总结Report的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!