目前,我有一个观点,显示我所有的投资到期在未来5年内,为每个客户。我想做的是显示每个金融机构的总额,如下所示:
机构|金额|到期金额
TD | 1000 | 1250
苏格兰| 1203 | 9383
等等
这是我用来显示未来5年到期投资价值的代码。你知道吗
#get the invesments maturing this year
for p in plans:
cur_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = current_year)
nxt_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = next_yr)
thr_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = thr_yr)
fr_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = fr_yr)
fv_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = fv_yr)
for inv in cur_inv:
total += inv.amount or 0
for inv in cur_inv:
total_m += inv.maturity_amount or 0
for inv in nxt_inv:
total2 += inv.amount or 0
for inv in nxt_inv:
total_m2 += inv.maturity_amount or 0
for inv in thr_inv:
total3 += inv.amount or 0
for inv in thr_inv:
total_m3 += inv.maturity_amount or 0
for inv in fr_inv:
total4 += inv.amount or 0
for inv in fr_inv:
total_m4 += inv.maturity_amount or 0
plan_list.append({
'plan':p,
'investment': cur_inv,
'nxt_inv': nxt_inv,
'thr_inv': thr_inv,
'fr_inv': fr_inv,
'fv_inv': fv_inv,
})
我使用一个列表来存储我的所有计划,这样我就可以为模板上的每个计划运行for循环。你知道吗
我有一个类似的观点,我希望这一个做什么,但只有一个计划,我不知道如何让它与多个计划的工作。你知道吗
这是我的代码,如果它只是一个选定的计划。你知道吗
#Calculate the holding totals with each company
total_list = plan.investment_set.filter(maturity_date__gte= '%s-1-1' % current_year).values('financial_institution__abbr').annotate(Sum('maturity_amount')).order_by('financial_institution__abbr')
context['list'] = total_list
如果有人能帮忙,我将不胜感激。你知道吗
谢谢。你知道吗
看起来您可能可以让您的数据库为您计算这个。你知道吗
在SQL中,这可能类似于:
看看Django's documentation for aggregation,看看有没有适合你的问题。你知道吗
另一方面,Django的queryset api是generative,因此您可以简化查询的构建:
编辑:我自己还没有尝试过,但是聚合文档的this part看起来是一个好的开始。你知道吗
相关问题 更多 >
编程相关推荐