在列表上使用注释

时间:2011-11-01 14:12:27

标签: python django django-templates django-views

目前,我有一个观点,显示我的所有投资在未来5年内为每个客户成熟。我想做的是显示每个金融机构的总数,显示如下:

机构|金额|到期金额

TD | 1000 | 1250
Scotia | 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

如果有人能提供帮助,那将非常感激。

感谢。

1 个答案:

答案 0 :(得分:0)

看起来您可以让您的数据库为您计算。

在SQL中,这可能类似于:

SELECT sum(investment.amount)
FROM investment, maturity_date
WHERE investment.id = maturity_date.investment_id and year=%s
GROUP BY investment.amount

查看Django's documentation for aggregation并查看是否适合您的问题。

另外,Django的queryset api是generative,因此您可以简化查询的构建:

investments = Investment.objects.all().filter(plan = p).order_by('financial_institution')
cur_inv = investments.filter(maturity_date__year = current_year)
nxt_inv = investments.filter(maturity_date__year = next_yr)
thr_inv = investments.filter(maturity_date__year = thr_yr)
fr_inv = investments.filter(maturity_date__year = fr_yr)
fv_inv = investments.filter(maturity_date__year = fv_yr)

编辑:我自己没有尝试过,但聚合文档的this part看起来是一个好的开始。