django annotate - 条件计数
django annotate - conditional count
我有一个名为 'StoreItem' 的模型和一个名为 'QuoteItem' 的模型。 QuoteItem 指向 StoreItem。
我正在尝试注释一个计数器,说明有多少报价项目指向商店项目,但有条件适用于报价项目。
我试过这样的事情:
items = items.annotate(
quote_count=Count(
Case(
When(quoteitem__lookup_date__in=this_week, then=1),
output_field=IntegerField()
)
)
)
'items' 是 StoreItems 的查询集。 'this_week' 是代表本周的日期列表(这是我尝试应用的过滤器)。在我使日期正常工作后,我想为这个条件计数添加更多过滤器,但让我们从那个开始。
无论如何,我得到的更像是一个布尔值 - 如果存在符合条件的报价项目,无论我有多少,计数器都将为 1。否则,将为 0。
看起来 Count(Case())
只检查是否存在任何项目,如果存在 return 1,而我希望它遍历指向商店项目的所有报价项目并计算它们,如果他们(单独)符合条件。
我该如何实现?
您需要将所有内容包装在 Sum
语句中,而不是 Count
(我发现 Count
完全起作用有点奇怪):
from django.db.models import Case, IntegerField, Sum, When
items = items.annotate(
quote_count=Sum(
Case(
When(quoteitem__lookup_date__in=this_week, then=1),
output_field=IntegerField()
)
)
)
这基本上是将内部 Case
语句的所有 0
和 1
相加,得出匹配数的计数。
我正在做类似的任务。对我来说,Sum
在 Case/When
上无法正常工作,因为我加入了多少张表(数太多了)。结果是这样的:
from django.db.models import Case, IntegerField, Count, When, F
items = items.annotate(
quote_count=Count(
Case(
When(quoteitem__lookup_date__in=this_week, then=F('quoteitem__id'),
),
distinct=True,
)
)
在我的例子中,我实际上必须将两个 Count
加在一起,例如:
items = items.annotate(
quote_count=Count(
Case(
When(quoteitem__lookup_date__in=this_week, then=F('quoteitem__id'),
),
distinct=True,
)
) + Count (
Case(
When(itemgroup__lookup_date__in=this_week, then=F('itemgroup__quoteitem__id'),
),
distinct=True,
)
假设 items
可以通过 itemgroup
或直接与 quoteitems
相关。
我有一个名为 'StoreItem' 的模型和一个名为 'QuoteItem' 的模型。 QuoteItem 指向 StoreItem。
我正在尝试注释一个计数器,说明有多少报价项目指向商店项目,但有条件适用于报价项目。
我试过这样的事情:
items = items.annotate(
quote_count=Count(
Case(
When(quoteitem__lookup_date__in=this_week, then=1),
output_field=IntegerField()
)
)
)
'items' 是 StoreItems 的查询集。 'this_week' 是代表本周的日期列表(这是我尝试应用的过滤器)。在我使日期正常工作后,我想为这个条件计数添加更多过滤器,但让我们从那个开始。
无论如何,我得到的更像是一个布尔值 - 如果存在符合条件的报价项目,无论我有多少,计数器都将为 1。否则,将为 0。
看起来 Count(Case())
只检查是否存在任何项目,如果存在 return 1,而我希望它遍历指向商店项目的所有报价项目并计算它们,如果他们(单独)符合条件。
我该如何实现?
您需要将所有内容包装在 Sum
语句中,而不是 Count
(我发现 Count
完全起作用有点奇怪):
from django.db.models import Case, IntegerField, Sum, When
items = items.annotate(
quote_count=Sum(
Case(
When(quoteitem__lookup_date__in=this_week, then=1),
output_field=IntegerField()
)
)
)
这基本上是将内部 Case
语句的所有 0
和 1
相加,得出匹配数的计数。
我正在做类似的任务。对我来说,Sum
在 Case/When
上无法正常工作,因为我加入了多少张表(数太多了)。结果是这样的:
from django.db.models import Case, IntegerField, Count, When, F
items = items.annotate(
quote_count=Count(
Case(
When(quoteitem__lookup_date__in=this_week, then=F('quoteitem__id'),
),
distinct=True,
)
)
在我的例子中,我实际上必须将两个 Count
加在一起,例如:
items = items.annotate(
quote_count=Count(
Case(
When(quoteitem__lookup_date__in=this_week, then=F('quoteitem__id'),
),
distinct=True,
)
) + Count (
Case(
When(itemgroup__lookup_date__in=this_week, then=F('itemgroup__quoteitem__id'),
),
distinct=True,
)
假设 items
可以通过 itemgroup
或直接与 quoteitems
相关。