1 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊
計(jì)數(shù)聚合有一個(gè)不同的參數(shù),可能會(huì)有所幫助:
>>> q = Book.objects.annotate(Count('authors', distinct=True), Count('store', distinct=True))
https://docs.djangoproject.com/en/3.1/topics/db/aggregation/#combining-multiple-aggregations
因此您的查詢將如下所示:
# I removed the distinct after .values, as the values works
# like a GROUP BY, thus you will get already unique 'cid's
total_actions_per_cid = Fruit.objects\
.filter(client=user)\
.values('cid') \
.annotate(num_uids=Count('uid', distinct=True))
添加回答
舉報(bào)