1

Group by count 值未显示 rest api 调用,但 group by 工作完美 这是我的 views.py 文件

class TestList(generics.ListAPIView):
    queryset = Test.objects.raw('SELECT  1 as id ,v1 ,COUNT(*)  FROM posts_test GROUP BY v1 order by count(*) desc  LIMIT 10')
    serializer_class = TestSerializer

    def list(self, request):
        queryset = self.get_queryset()
        # the serializer didn't take my RawQuerySet, so made it into a list
        serializer = TestSerializer(list(queryset), many=True)
        return Response(serializer.data)

输出 :

[{"v1":"fg","v2":"fg","v3":"fg"},
 {"v1":"nn","v2":"fg","v3":"fg"}, 
 {"v1":"kk","v2":"fg","v3":"fg"}]

但我想要这样:

[{"v1":"fg","v2":"fg","v3":"fg","count":20,},
 {"v1":"nn","v2":"fg","v3":"fg","count":15,},
 {"v1":"kk","v2":"fg","v3":"fg","count":10,}]
4

0 回答 0