annotate() 添加超出需要的内容

发布于 2024-12-12 20:15:31 字数 2393 浏览 1 评论 0原文

我的 annotate(sum()) 函数遇到一个小问题。现在我想要它做的是显示列表内给定计划中所有到期日的总计。对于第一个来说它确实如此。代码如下:

#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

            for inv in fv_inv:
                total5 += inv.amount or 0
            for inv in fv_inv:
                total_m5 += inv.maturity_amount or 0

#Calculate the holding totals with each company
            total_list = p.investment_set.filter(maturity_date__gte= '%s-1-1' % current_year).values('financial_institution__abbr').annotate(Sum('maturity_amount')).order_by('financial_institution__abbr')
            gtotal = total_m + total_m2 + total_m3 + total_m4 + total_m5

            plan_list.append({
                'plan':p,
                'investment': cur_inv,
                'nxt_inv': nxt_inv,
                'thr_inv': thr_inv,
                'fr_inv': fr_inv,
                'fv_inv': fv_inv,
                'total_list': total_list,
                'grand': gtotal,
            })

我现在唯一的问题是,当它进入下一个计划时,它会继续添加到总计中,而不是回到 0。

我错过了什么吗?

任何帮助将不胜感激。

谢谢

I'm having a small issue with my annotate(sum()) function. Now what I want it to do is show a total for all maturities in the given plan, inside the list. Which for the first one it does. Code below:

#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

            for inv in fv_inv:
                total5 += inv.amount or 0
            for inv in fv_inv:
                total_m5 += inv.maturity_amount or 0

#Calculate the holding totals with each company
            total_list = p.investment_set.filter(maturity_date__gte= '%s-1-1' % current_year).values('financial_institution__abbr').annotate(Sum('maturity_amount')).order_by('financial_institution__abbr')
            gtotal = total_m + total_m2 + total_m3 + total_m4 + total_m5

            plan_list.append({
                'plan':p,
                'investment': cur_inv,
                'nxt_inv': nxt_inv,
                'thr_inv': thr_inv,
                'fr_inv': fr_inv,
                'fv_inv': fv_inv,
                'total_list': total_list,
                'grand': gtotal,
            })

My only issue now, is that when it goes to the next plan, it continues to add to the grand total, instead of going back to 0.

Am I missing something?

Any help would be appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-12-19 20:15:31

您将 +=total_m* 变量一起使用,但从未在循环中将它们重置为 0。它们不会仅仅因为新的迭代已经开始而自动重置。

FWIW,您应该尝试在这里优化您的代码。您正在生成 6*len(plans) 查询,这可能会相当昂贵。

You're using += with the total_m* vars but never resetting them to 0 in your loop. They don't automatically reset, just because a new iteration has started.

FWIW, you should try to optimize your code here. You're generating 6*len(plans) queries, which could be rather costly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文