python测试开发django-rest-framework-86.分页功能(PageNumberPagination)
2021-03-06 14:28
标签:local numbers 简单 class 作者 控制 time success dsc 当查询出来的数据量非常大的时候,需要分页查询,django-rest-framework 提供了分页的支持。 django-rest-framework 分页器有三种: 我们可以在配置文件中设置全局的分页方式, 这里设置简单分页(PageNumberPagination),每页显示10条数据 也可以自己重写PageNumberPagination类,定义分页方式 PageNumberPagination类可以设置以下属性 写查询视图的时候,先序列号,再定义分页器,查询的时候可以带上参数 如果不带参数,直接请求,会返回默认的5条数据 带上page和size参数查询,指定查询的page页数和size数量 python测试开发django-rest-framework-86.分页功能(PageNumberPagination) 标签:local numbers 简单 class 作者 控制 time success dsc 原文地址:https://www.cnblogs.com/yoyoketang/p/14296505.html前言
有三种分页功能:PageNumberPagination,LimitOffsetPagination,CursorPagination。分页器
简单分页(PageNumberPagination)
REST_FRAMEWORK = {
‘DEFAULT_PAGINATION_CLASS‘: ‘rest_framework.pagination.PageNumberPagination‘,
‘PAGE_SIZE‘: 5 # 每页数目
}
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 # 后台控制显示的最大记录条数
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
})
分页查询
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
}]
}
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
}]
}
上一篇:算法模板 - C++ 高精度运算
下一篇:Python学习-第一遍QA
文章标题:python测试开发django-rest-framework-86.分页功能(PageNumberPagination)
文章链接:http://soscw.com/essay/60895.html