在不同的 Django 查询集上注释不再使用不同的查询集

Annotating on a distinct Django Queryset is no longer using the distinct queryset

我有一个查询,我正在尝试为字段 tail_tip 注释每个值的计数。我的原始查询在相关 table 上进行过滤,因此有必要使用 distinct()。 我不太确定如何描述,但是当我注释不同的查询时,它会出现查询不再不同。这是我在 shell.

中使用的查询
// Without distinct()
Ski.objects.filter(published=True, size__size__in=[178, 179, 180, 181, 182, 183, 184]).count()
// 318

skis = Ski.objects.filter(published=True, size__size__in=[178, 179, 180, 181, 182, 183, 184]).distinct()
skis.count()   
// 297

skis.values('tail_tip').order_by('tail_tip').annotate(count=Count('tail_tip'))
// <QuerySet [{'tail_tip': 'flat', 'count': 99}, {'tail_tip': 'full_twin', 'count': 44}, {'tail_tip': 'partial_twin', 'count': 175}]>
// total count = 318

鉴于 skis 已经是 distinct() 我不知道为什么当我注释它时总计数等于非不同查询。

假设名为 SizeSki 的相关 table 与其一对多。这可以通过以下方式完成:

ski_filtered = Ski.objects.filter(
    published=True, 
    id__in=Size.objects.values("skis__id").filter(  # Maybe somethig else than `skis` here, you didn't show its model, so I couldn't know
        size__in=[178, 179, 180, 181, 182, 183, 184]
    )
)

skis.values('tail_tip').distinct().order_by('tail_tip').annotate(count=Count('tail_tip'))

原因:DISTINCT 不能与 annotate() 一起使用。 annotate() 在 django 中的实现是用它自己的 GROUP BY 子句实现的。因此,在已经 GROUP BY 的查询上应用 distinct() 是行不通的。

尝试添加 distinct=True:

skis.values('tail_tip').order_by('tail_tip').annotate(count=Count('tail_tip', distinct=True))