在我的 DRF API 中,我有这样的视图
class ActivityAPI(viewsets.ModelViewSet):
authentication_classes = (SessionAuthentication, TokenAuthentication)
serializer_class = ActivitySerializer
queryset = Activity.objects.order_by('-id').all()
filter_backends = (DjangoFilterBackend,)
filter_class = ActivityFilter
filter_fields = ('name', 'ack', 'developer', 'ack_date', 'ack_by', 'verb')
def get_count(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
if CASE_1:
queryset = queryset.values('verb').annotate(count=Count('verb'))
if CASE_2:
queryset = Activity.objects.values('verb').annotate(count=Count('verb'))
return Response(data=queryset)
在CASE_2中,我得到了我所期望的,这相当于 SQL 查询 SELECT
activity_activity .
verb , COUNT(
activity_activity .
verb ) AS
count FROM
activity_activity GROUP BY
activity_activity .
verbORDER BY NULL
但是当涉及到CASE_1时,注释功能将查询集分组activity.id
,即SELECT
activity_activity .
verb , COUNT(
activity_activity .
verb ) AS
count FROM
activity_activity GROUP BY
activity_activity .
id ORDER BY
activity_activity .
idDESC
注意我需要 API 和聚合的基于 url 的过滤数据