如何将distinct()与F()表达式一起使用



我的计数在此查询中包含重复项:

items = items.annotate(
user_id=F("product__items__user_id"),
start_month=TruncMonth("start")
) 
.values("user_id", "start_month") 
.annotate(count=Count("start_month"))

我需要以某种方式将distinct()应用于F表达式,以避免重复,这可能吗?

显然Count有一个不同的参数,在这种情况下需要设置:

items = items.annotate(
user_id=F("product__items__user_id"),
start_month=TruncMonth("start")
) 
.values("user_id", "start_month") 
.annotate(count=Count("start_month", distinct=True))

所以我不需要带F((表达式的distinct(((我认为这是不可能的,在另一种情况下,我会尝试使用Subquery(

最新更新