繁体   English   中英

当日期在一个范围内时计算每次出现的次数

[英]count each occurrences when date falls within a range

我试图计算每次created_on中的月份值落在不同的月份内,使用下面的 django orm:

    result_qs = Model.objects.all().annotate(
        spring=Count(Case(When(ExtractMonth("created_on") in [1, 2, 3, 4, 5], then=1))),
        summer=Count(Case(When(ExtractMonth("created_on") in [6, 7], then=1))),
        fall=Count(Case(When(ExtractMonth("created_on") in [8, 9, 10, 11, 12], then=1))),
        year=ExtractYear("created_on")
)

我收到错误

When() 支持 Q object、boolean 表达式或查找作为条件。

当我将Q符号应用于其中一种情况时,例如:

spring=Count(Case(When(Q(ExtractMonth("created_on") in [1, 2, 3, 4, 5], then=1)))),

无法解压不可迭代的布尔值 object

有什么建议么?

你可以尝试这样的事情:

result_qs = Model.objects.all().annotate(
        spring=Count(Case(When(created_on__month__in=[1, 2, 3, 4, 5], then=1))),
        summer=Count(Case(When(created_on__month__in=[6, 7], then=1))),
        fall=Count(Case(When(created_on__month__in=[8, 9, 10, 11, 12], then=1))),
        year=ExtractYear("created_on")
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM