【问题标题】:Using annotate on list在列表上使用注释
【发布时间】:2011-12-19 13:07:05
【问题描述】:

目前,我有一个视图显示我的所有投资将在未来 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

如果有人能提供帮助,将不胜感激。

谢谢。

【问题讨论】:

    标签: python django django-templates django-views


    【解决方案1】:

    看起来你可以让你的数据库为你计算这个。

    在 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 的查询集 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 看起来是一个好的开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2018-05-24
      • 1970-01-01
      • 2011-04-04
      • 2014-06-08
      • 1970-01-01
      • 2020-01-20
      相关资源
      最近更新 更多