1.编写分页器方法
from rest_framework import pagination class Pagination(pagination.PageNumberPagination): def get_paginated_data(self, data): return { "total": self.page.paginator.count, "data": data } def get_paginated_response(self, data): return build_ret(total=self.page.paginator.count, data=data)
2.在Django的Settings里面添加配置
REST_FRAMEWORK = { # 下面的Pagination就是上面写的类文件地址 'DEFAULT_PAGINATION_CLASS': 'util.pagination.Pagination', 'PAGE_SIZE': 10 }
3.配合序列化器一起使用
from django.http import JsonResponse class PlanView(ListAPIView): """ 计划列表 """ queryset = user_model.Plan.objects.filter().order_by("-id") serializer_class = PlanSerializer def list(self, request, *args, **kwargs): data_list = self.paginate_queryset(self.filter_queryset(self.get_queryset())) serializer = PlanSerializer(data_list, many=True) return JsonResponse("total": self.paginator.page.paginator.count, "data": serializer.data)
标签:paginator,分页,self,queryset,Django,序列化,data,serializer From: https://www.cnblogs.com/SlookUp/p/17000145.html