前言
当查询出来的数据量非常大的时候,需要分页查询,django-rest-framework 提供了分页的支持。
有三种分页功能:PageNumberPagination,LimitOffsetPagination,CursorPagination。
分页器
django-rest-framework 分页器有三种:
- PageNumberPagination 简单分页(查看第n页,每页显示N条)
- LimitOffsetPagination 偏移分页(在第n个位置,向后查看n条数据)
- CursorPagination 加密分页,只能看上一页和下一页,速度快
简单分页(PageNumberPagination)
我们可以在配置文件中设置全局的分页方式, 这里设置简单分页(PageNumberPagination),每页显示10条数据
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 5 # 每页数目
}
也可以自己重写PageNumberPagination类,定义分页方式
from rest_framework.pagination import PageNumberPagination
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
class MyPageNumberPagination(PageNumberPagination):
page_size = 5 # 默认每页显示的多少条记录
page_query_param = 'page' # 默认查询参数名为 page
page_size_query_param = 'size' # 前台控制每页显示的最大条数
max_page_size = 20 # 后台控制显示的最大记录条数
PageNumberPagination类可以设置以下属性
- page_size 数字,页面显示的记录条数,不设置的就默认setting配置的全局PAGE_SIZE值
- page_query_param 字符串,查询参数的名称,不设置默认是'page'
- page_size_query_param 字符串,请求设置页面大小的参数名称,默认是None
- max_page_size 最大允许请求的页面大小,仅在page_size_query_param被设置时有效
- last_page_string 字符串列表或元组,默认是 ('last',)
- template 分页空间使用的模板名称,可以覆盖或设置为None,默认'rest_framework/pagination/numbers.html'
APIView使用分页查询
写查询视图的时候,先序列化,再定义分页器,查询的时候可以带上参数/api/v1/goods?page=1&size=10
from rest_framework import serializers
from .models import Goods
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated, AllowAny
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
# 序列化 Goods 模型
class GoodsAPISerializer(serializers.ModelSerializer):
class Meta:
model = Goods
fields = '__all__' # 返回全部的字段
# 定义分页器简单分页(PageNumberPagination)
class MyPageNumberPagination(PageNumberPagination):
page_size = 5 # 默认每页显示的多少条记录
page_query_param = 'page' # 默认查询参数名为 page
page_size_query_param = 'size' # 前台控制每页显示的最大条数
max_page_size = 20 # 后台控制显示的最大记录条数
# 查询视图
class GoodsAPISView(APIView):
permission_classes = (AllowAny,) # AllowAny 允许所有用户
def get(self, request, *args, **kwargs):
'''返回所有的'''
page = MyPageNumberPagination()
goods = Goods.objects.all() # 查询全部
ret = page.paginate_queryset(goods, request)
serializer = GoodsAPISerializer2(ret, many=True)
return Response({
"code": 0,
"msg": "success!",
"data": serializer.data
})
分页查询
如果不带参数,直接请求,会返回默认的 5 条数据http://localhost:8000/api/v1/goods
{
"code": 0,
"msg": "success!",
"data": [{
"id": 1,
"create_time": "2021-01-17 15:14:25",
"update_time": "2021-01-19 10:02:05",
"goodsname": "《selenium入门到精通3》",
"goodscode": "sp_100049",
"merchantid": "10001",
"merchantname": "悠悠学堂",
"goodsprice": 52.365,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 13,
"create_time": "2021-01-16 20:12:36",
"update_time": "2021-01-17 09:41:05",
"goodsname": "《selenium入门到精通》",
"goodscode": "sp_100008",
"merchantid": "10001",
"merchantname": "悠悠学堂",
"goodsprice": 49.9,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 14,
"create_time": "2021-01-16 20:12:42",
"update_time": "2021-01-17 09:41:05",
"goodsname": "《selenium入门到精通》",
"goodscode": "sp_100009",
"merchantid": "10001",
"merchantname": "悠悠学堂",
"goodsprice": 49.9,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 101,
"create_time": "2021-01-16 21:24:10",
"update_time": "2021-01-17 09:41:05",
"goodsname": "",
"goodscode": "sp_10086",
"merchantid": "",
"merchantname": "",
"goodsprice": 0.0,
"stock": 0,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 102,
"create_time": "2021-01-16 21:26:23",
"update_time": "2021-01-17 09:41:05",
"goodsname": "",
"goodscode": "sp_10087",
"merchantid": "",
"merchantname": "",
"goodsprice": 0.0,
"stock": 0,
"goodsgroupid": 0,
"goodsstatus": 1
}]
}
带上 page 和 size 参数查询,指定查询的 page 页数和 size 数量http://localhost:8000/api/v1/goods?page=1&size=2
{
"code": 0,
"msg": "success!",
"data": [{
"id": 1,
"create_time": "2021-01-17 15:14:25",
"update_time": "2021-01-19 10:02:05",
"goodsname": "《selenium入门到精通3》",
"goodscode": "sp_100049",
"merchantid": "10001",
"merchantname": "悠悠学堂",
"goodsprice": 52.365,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}, {
"id": 13,
"create_time": "2021-01-16 20:12:36",
"update_time": "2021-01-17 09:41:05",
"goodsname": "《selenium入门到精通》",
"goodscode": "sp_100008",
"merchantid": "10001",
"merchantname": "悠悠学堂",
"goodsprice": 49.9,
"stock": 100,
"goodsgroupid": 0,
"goodsstatus": 1
}]
}