在分页后新增字段

重写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
  }
}

你可能感兴趣的:(在分页后新增字段)