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'))
精彩评论