https://github.com/Coxhuang/django-Pagination
Python3.6
Django==2.0.7
djangorestframework==3.8.2
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from collections import OrderedDict
class MyCustomPagination(PageNumberPagination):
page_size = 5 # 每页显示多少个
page_size_query_param = "size" # 默认每页显示3个,可以通过传入pager1/?page=2&size=4,改变默认每页显示的个数
max_page_size = 100 # 最大页数不超过100
page_query_param = "page" # 获取页码数的
def get_paginated_response(self, data):
"""输出格式"""
return Response(OrderedDict([
('count', self.page.paginator.count), # 整个数据的个数
('success', True), # 验证消息
('next', self.get_next_link()), # 下一页url
('previous', self.get_previous_link()), # 上一页url
('results', data) # 当前页的数据
]))
视图
class get_view(mixins.CreateModelMixin,
mixins.ListModelMixin,
GenericViewSet):
pagination_class = None # 没有使用分页(也可以注释掉)
queryset = models.Student.objects.all()
serializer_class = get_sreializer
接口数据
[
{
"id": 1,
"name": "cox",
"age": "12"
},
{
"id": 2,
"name": "cox",
"age": "12"
},
{
"id": 3,
"name": "cox",
"age": "12"
},
{
"id": 4,
"name": "cox",
"age": "12"
},
...
...
...
]
视图
from app.pagination import MyCustomPagination
class get_view(mixins.CreateModelMixin,
mixins.ListModelMixin,
GenericViewSet):
pagination_class = MyCustomPagination # 使用刚刚自定义的分页
queryset = models.Student.objects.all()
serializer_class = get_sreializer
接口数据
{
"count": 11,
"success": true,
"next": "http://127.0.0.1:8000/get_data/?page=2",
"previous": null,
"results": [
{
"id": 1,
"name": "cox",
"age": "12"
},
{
"id": 2,
"name": "cox",
"age": "12"
},
{
"id": 3,
"name": "cox",
"age": "12"
},
{
"id": 4,
"name": "cox",
"age": "12"
},
{
"id": 5,
"name": "cox",
"age": "12"
}
]
}
需求:
如何处理Z接口
尝试:
视图
from app.pagination import MyCustomPagination
class MyChildCustomPagination(MyCustomPagination):
page_size = 1 # 每页显示多少个
class get_view(mixins.CreateModelMixin,
mixins.ListModelMixin,
GenericViewSet):
pagination_class = MyChildCustomPagination # 子类
queryset = models.Student.objects.all()
serializer_class = get_sreializer
接口数据
{
"count": 11,
"success": true,
"next": "http://127.0.0.1:8000/get_data/?page=2",
"previous": null,
"results": [
{
"id": 1,
"name": "cox",
"age": "12"
}
]
}