无法使用django查询进行分组
我有这个查询,但它会为每个水果返回单独的结果。如何获得分组结果无法使用django查询进行分组,django,django-models,django-orm,Django,Django Models,Django Orm,我有这个查询,但它会为每个水果返回单独的结果。如何获得分组结果 Fruit.objects.\ annotate(quantity=Count('fruit_box')).\ annotate(remaining=Count('pk', filter=~Q(fruit_status='eaten'))).\ annotate(eaten=Subquery(ShipmentContent
Fruit.objects.\
annotate(quantity=Count('fruit_box')).\
annotate(remaining=Count('pk', filter=~Q(fruit_status='eaten'))).\
annotate(eaten=Subquery(ShipmentContent.objects.filter(
object_id=OuterRef('pk'),\
fruits__fruit_box=OuterRef('fruit_box'),\
shipment=self.request.query_params.get('shipment_id'),\
fruits__fruit_status='eaten',\
content_type=fruit_content_type).\
values('object_id').\
annotate(count=Count('object_id')).\
values('count')\
)\
).\
annotate(location=F('fruit_box__location__description')).\
values('fruit_box__label', 'fruit_box', 'harvest_date', 'seeded').\
order_by('fruit_box__label')
我不确定您到底想要什么,但对于分组,您需要使用
aggregate
而不是annotate
。我不确定这些信息是否足以完全回答您的问题。你想按什么分组?您可能只需要将.values(…)
调用放在.annotate(…)
调用之前。