开发者

Calculating the sum of aggregate query set

So I have a query set that returns a list of financial institutions and also the total amount of money in investments I have with each institution.

views.py

total_list = plan.investment_set.filter(maturity_date__gte= '%s-1-1' % current_year).values('financial_institution__abbr').annotate(Sum('maturity_amount'))

What I would like to do now, is find a grand total for all that money. My question is, how do I go about calculating the开发者_运维技巧 sum to find the total of all my investments.


I believe you're looking for .aggregate():

total_list = plan.investment_set.filter(
    maturity_date__gte= '%s-1-1' % current_year
).aggregate(Sum('maturity_amount'))

which should give you:

{'maturity_amount__sum': <total amount>}


Sorry this is creating a hassle. Is the field that you're calculating maturity_amount named value? If so, you may try this which worked for your previous question (Using Django to summarize Report):

grand_total = plan.investment_set.annotate(Sum('value'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜