分页
继承PageNumberPagination
即通过获取页码和每页显示的数量进行显示
简单示例
from rest_framework.pagination import PageNumberPagination
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.views import APIView
from api.models import Role
class PageSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = '__all__'
class PageView(APIView):
def get(self, request, *args, **kwargs):
# 1、去数据库拿取所有数据
roles = Role.objects.all()
# 2、创建分页对象
pg = PageNumberPagination()
# 3、将数据库中的数据进行分页处理
page_roles = pg.paginate_queryset(queryset=roles, request=request, view=self)
# 4、序列化分页后的对象
ser = PageSerializer(instance=page_roles, many=True)
return Response(ser.data)
自定义分页处理
from rest_framework.pagination import PageNumberPagination
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.views import APIView
from api.models import Role
class MyPageSerializer(PageNumberPagination):
'''自定义分页类'''
# http://127.0.0.1:8000/api/v1/page/?page=3&size=3
# 设置显示页数的参数
page_query_param = 'page' # ?page=3&size=3 中的page名
# 设置每页显示数量,每页显示三条数据
page_size = 3
# 设置每页显示数量的参数
page_size_query_param = 'size' # ?page=3&size=3 中的size名
# 设置每页最大显示数量
max_page_size = 5
class PageView(APIView):
def get(self, request, *args, **kwargs):
# 去数据库拿取所有数据
roles = Role.objects.all()
# 创建自定义分页对象
pg = MyPageSerializer()
# 将数据库中的数据进行分页处理
page_roles = pg.paginate_queryset(queryset=roles, request=request, view=self)
# 序列化分页后的对象
ser = PageSerializer(instance=page_roles, many=True)
return Response(ser.data)
url:http://127.0.0.1:8000/api/v1/page/?page=2&size=3
结果
[
{
"id": 4,
"title": "程序员"
},
{
"id": 5,
"title": "律师"
},
{
"id": 6,
"title": "学生"
}
]
改变返回结果
class PageView(APIView):
def get(self, request, *args, **kwargs):
# 1、去数据库拿取所有数据
roles = Role.objects.all()
# 2、创建分页对象
pg = MyPageSerializer()
# 3、将数据库中的数据进行分页处理
page_roles = pg.paginate_queryset(queryset=roles, request=request, view=self)
# 4、序列化分页后的对象
ser = PageSerializer(instance=page_roles, many=True)
# return Response(ser.data)
return pg.get_paginated_response(ser.data)
url:http://127.0.0.1:8000/api/v1/page/?page=2&size=3
结果
{
"count": 8,
"next": "http://127.0.0.1:8000/api/v1/page/?page=3&size=3",
"previous": "http://127.0.0.1:8000/api/v1/page/?size=3",
"results": [
{
"id": 4,
"title": "程序员"
},
{
"id": 5,
"title": "律师"
},
{
"id": 6,
"title": "学生"
}
]
}
继承LimitOffsetPagination
即通过获取任意的一条数据作为起始位置和每页显示的数量进行显示
from rest_framework.pagination import LimitOffsetPagination
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.views import APIView
from api.models import Role
class MyLimitSerializer(LimitOffsetPagination):
# http://127.0.0.1:8000/api/v1/page/?offset=3&limit=3 从第三条数据进行显示,每页显示三条数据
# 设置最大显示的参数
limit_query_param = 'limit'
# 设置从哪一条数据显示的参数
offset_query_param = 'offset'
# 默认每页显示数量
default_limit = 3
# 设置每页最大显示数量
max_limit = 5
class PageView(APIView):
def get(self, request, *args, **kwargs):
roles = Role.objects.all()
pg = MyLimitSerializer()
page_roles = pg.paginate_queryset(queryset=roles, request=request, view=self)
ser = PageSerializer(instance=page_roles, many=True)
return Response(ser.data)
url:http://127.0.0.1:8000/api/v1/page/?offset=3&limit=3
结果
[
{
"id": 4,
"title": "程序员"
},
{
"id": 5,
"title": "律师"
},
{
"id": 6,
"title": "学生"
}
]
继承CursorPagination
即通过对页码进行加密,并通过记住前一页和后一页的页码进行展示数据
from rest_framework.pagination import CursorPagination
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.views import APIView
from api.models import Role
class MyCursorSerializer(CursorPagination):
# 设置页码的参数
cursor_query_param = 'cursor'
# 设置每页显示的个数
page_size = 2
# 设置按id进行排序,记住每页的最大值和最小值,进行翻页是先进行比较,直接找到最大的数据,然后进行展示
ordering = 'id'
# 设置最大每页显示个数
max_page_size = 3
class PageView(APIView):
def get(self, request, *args, **kwargs):
roles = Role.objects.all()
pg = MyCursorSerializer()
page_roles = pg.paginate_queryset(queryset=roles, request=request, view=self)
ser = PageSerializer(instance=page_roles, many=True)
# return Response(ser.data)
return pg.get_paginated_response(ser.data)
url:http://127.0.0.1:8000/api/v1/page/
结果
{
"next": "http://127.0.0.1:8000/api/v1/page/?cursor=cD0y",
"previous": null,
"results": [
{
"id": 1,
"title": "医生"
},
{
"id": 2,
"title": "护士"
}
]
}
获取前一页和后一页的链接后,进行翻页处理
url:http://127.0.0.1:8000/api/v1/page/?cursor=cD0y
结果
{
"next": "http://127.0.0.1:8000/api/v1/page/?cursor=cD00",
"previous": "http://127.0.0.1:8000/api/v1/page/?cursor=cj0xJnA9Mw%3D%3D",
"results": [
{
"id": 3,
"title": "老师"
},
{
"id": 4,
"title": "程序员"
}
]
}