美文网首页
在分页后新增字段

在分页后新增字段

作者: lkning | 来源:发表于2018-04-09 16:47 被阅读0次

    重写list方法

        def list(self, request, *args, **kwargs):
            queryset = self.filter_queryset(self.get_queryset())
            page = self.paginate_queryset(queryset)
            undiposed_count = Maintain.objects.filter(Q(shop=request.user.shop) & Q(status='NOT_PROCESS')).count()
            diposed_count = Maintain.objects.filter(Q(shop=request.user.shop) & ~Q(status='NOT_PROCESS')).count()
            #undiposed_count ,diposed_count 为新增字段
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                respone = self.get_paginated_response(serializer.data)
                respone.data["undiposed_count"] = undiposed_count
                respone.data["diposed_count"] = diposed_count
                return respone
    
            serializer = self.get_serializer(queryset, many=True)
            return Response(serializer.data)
    
    {
      "code": 0,
      "msg": "请求成功",
      "data": {
        "count": 662,
        "next": "http://127.0.0.1:8000/api/maintain/m/?page=2&remain_m_mileage_max=30000&task=mileage",
        "previous": null,
        "results": [
          {
            "data":"name"
          }
        ],
        "undiposed_count": 663,
        "diposed_count": 137
      }
    }
    

    相关文章

      网友评论

          本文标题:在分页后新增字段

          本文链接:https://www.haomeiwen.com/subject/dkbihftx.html