使用Django总结报告 [英] Using Django to summarize Report

查看:116
本文介绍了使用Django总结报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试制作一张表格,显示计划中每个金融机构的到期日总金额。人。所以每个人都可以有多个投资。目前,我有一份报告,显示按到期日(即2011,2012等)分类的每笔投资。



在底部,我想放置此汇总表,但是由于能够进行多项投资,我的查询显示每个金融机构的重复项。



我当前的查询:

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

这将输出:




  • TD银行$ 10k

  • TD Bank $ 10k

  • Scotia Bank $ 10k

  • 等等



但我想:




  • TD银行$ 20k

  • 银行$ 10k

  • 等等


解决方案

你想要对于计划投资的价值观:

  from django.db.models import Sum 
my_plan.investment_set。过滤器(
maturity_date__gte = when
).values('financial_institution')。annotate(Sum('value'))


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

解决方案

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总结报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆