首页 > 解决方案 > Django:进行复杂注释和聚合的问题

问题描述

这是模型:

class Purchase(models.Model):
    date           = models.DateField(default=datetime.date.today,blank=False, null=True)
    total_purchase = models.DecimalField(max_digits=10,decimal_places=2,blank=True, null=True)

我想在特定日期范围内按月计算“total_purchase”,这样如果一个月内没有购买,则总购买量应该是上个月的购买价值,如果两个月内有购买,那么总购买量会不会加上这两个...

例子:

假设用户给出的日期范围是从 4 月到 11 月。

如果在 4 月购买了 2800 美元,在 8 月购买了 5000 美元,在 10 月购买了 6000 美元。

然后输出将是这样的:

April      2800
May        2800
June       2800
July       2800
August     7800  #(2800 + 5000)
September  7800
October    13800 #(7800 + 6000)
November   13800

知道如何在 django 查询中执行此操作吗?

谢谢

根据Raydel Miranda先生给出的答案。我做了以下

import calendar
import collections
import dateutil

start_date = datetime.date(2018, 4, 1)
end_date = datetime.date(2019, 3, 31)

results = collections.OrderedDict()

result = Purchase.objects.filter(date__gte=start_date, date__lt=end_date).annotate(real_total = Case(When(Total_Purchase__isnull=True, then=0),default=F('tal_Purchase')))

date_cursor = start_date

while date_cursor < end_date:
    month_partial_total = result.filter(date__month=date_cursor.month).agggate(partial_total=Sum('real_total'))['partial_total']

    results[date_cursor.month] = month_partial_total

    if month_partial_total == None:
            month_partial_total = int(0)
    else:
            month_partial_total = month_partial_total

    date_cursor += dateutil.relativedelta.relativedelta(months=1)

    return results

但是现在输出是这样的(来自上面的示例):

April      2800
May        0
June       0
July       0
August     5000
September  0
October    6000
November   0

有谁知道如何在几个月之间添加...我想做类似的事情

e = month_partial_total + month_partial_total.next

我想添加每个month_partial_total 的下一个迭代值。我想这会解决我的问题..

知道任何人如何在 django 中执行此操作吗?

谢谢

标签: djangodjango-modelsdjango-aggregationdjango-annotate

解决方案


我在您的问题中指出了两点:

  1. 结果按月排序。
  2. 总购买量可以是blanknull

基于这些事情,我将提出这种方法:

您可以获得给定月份的总数,您只需要处理 null 的情况(作为旁注,拥有where为 nulltotal_pushase的实例没有任何意义,至少它必须为 0)。Purchasetotal_purchase

阅读有关Django 条件表达式以了解有关When和的更多信息Case

# Annotate the filtered objects with the correct value (null) is equivalent
# to 0 for this requirement.

result = Purchase.objects.filter(date__gte=start_date, date__lt=end_date).annotate(
    real_total = Case(
        When(total_purchase__isnull=True, then=0),
        default=F('total_purchase')
    )
)

# Then if you want to know the total for a specific month, use Sum.
month_partial_total = result.filter(
    date__month=selected_month
).aggregate(
    partial_total=Sum('real_total')
)['partial_total']

您可以在函数中使用它来实现您想要的结果:

import calendar
import collections
import dateutil

def totals(start_date, end_date):
    """
    start_date and end_date are datetime.date objects.
    """

    results = collections.OrderedDict()  # Remember order things are added.

    result = Purchase.objects.filter(date__gte=start_date, date__lt=end_date).annotate(
        real_total = Case(
            When(total_purchase__isnull=True, then=0),
            default=F('total_purchase')
        )
    )

    date_cursor = start_date
    month_partial_total = 0
    while date_cursor < end_date:
        # The while statement implicitly orders results (it goes from start to end).
        month_partial_total += result.filter(date__month=date_cursor.month).aggregate(
            partial_total=Sum('real_total')
        )['partial_total']


        results[date_cursor.month] = month_partial_total

        # Uncomment following line if you want result contains the month names
        # instead the month's integer values.
        # result[calendar.month_name[month_number]] = month_partial_total

        date_cursor += dateutil.relativedelta.relativedelta(months=1)

    return results

由于 Django 1.11 可能能够解决这个问题SubQueries,但我从未将它用于同一模型上的子查询。


推荐阅读