新建自定义分页pager.py文件
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
from rest_framework.response import Response
'''
自定义分页:基础分页
http://127.0.0.1:8000/api/v1/work_order?size=10&page=1
'''
class MyPagination(PageNumberPagination):
# 指定每一页的个数,默认为配置文件里面的PAGE_SIZE,也可以在settings.py中添加全局配置('PAGE_SIZE': 10 )
page_size = 10
# 可以让前端指定每页个数,指定size做为key显示每页个数,可自定义key
page_size_query_param = 'size'
# 控制每页最大显示条数:(这个控制仅限制于路径后拼接设置size=1000后,对其进行限制)
max_page_size = 100
# 可以让前端指定页码数,默认是page做为key显示页数,可自定义key
page_query_param = 'page'
# # 指定返回格式,根据需求返回一个总页数,数据存在results字典里返回
# def get_paginated_response(self, data):
# from collections import OrderedDict
# return Response(OrderedDict([('count', self.page.paginator.count), ('data', data)]))
def get(self, request):
# 自定义response result,用于前后端分离
result = {'count': '', 'data': '', 'meta': {'msg': 'OK', 'status': 200}}
try:
# 基本分页
workorder = WorkOrderInfo.objects.all().order_by('id')
count = WorkOrderInfo.objects.count()
# 创建对象
pg = MyPagination()
# 获取分页数据
page_query = pg.paginate_queryset(queryset=workorder, request=request, view=self)
# 序列化数据
workorder_ser = WorkOrderSerializer(page_query, many=True)
result['data'] = workorder_ser.data
result['count'] = count
except Exception as e:
result['meta']['msg'] = '获取数据失败'
result['meta']['status'] = 500
return Response(result)
'''
自定义分页:Offset分页类
PS:http://127.0.0.1:8000/api/v1/work_order?limit=10&offset=20
'''
class MyLimitOffsetPagination(LimitOffsetPagination):
# 默认从offset偏移位置往后取几个
default_limit = 10
# 每页可显示的条数,可自定义key
limit_query_param = 'limit'
# offset(起始位置),从设置的offset值那个位置往后拿limit值的记录
offset_query_param = 'offset'
# # 设置最大获取条数
max_limit = 20
# def get_paginated_response(self, data):
# from collections import OrderedDict
# return Response(OrderedDict([('count', self.count), ('data', data)]))
def get(self, request):
# 自定义response result,用于前后端分离
result = {'count': '', 'data': '', 'meta': {'msg': 'OK', 'status': 200}}
try:
# offset分页
workorder = WorkOrderInfo.objects.all().order_by('id')
count = WorkOrderInfo.objects.count()
pg = MyLimitOffsetPagination()
page_query = pg.paginate_queryset(queryset=workorder, request=request, view=self)
workorder_ser = WorkOrderSerializer(page_query, many=True)
result['data'] = workorder_ser.data
result['count'] = count
except Exception as e:
result['meta']['msg'] = '获取数据失败'
result['meta']['status'] = 500
return Response(result)
'''
自定义分页:加密分页
加密分页方式,只能通过点“上一页”和下一页访问数据
'''
class MyCursorPagination(CursorPagination):
cursor_query_param = "cursor"
# 每页显示2个数据
page_size = 2
# 排序
ordering = 'id'
page_size_query_param = None
max_page_size = None
加密方法不能按照上两种常规方法做,需要通过return get_paginated_response 方法来获取上下页的加密页码,只有通过该页码才能访问对应页面。这里因为暂时用不到就先放下。
def get(self, request):
# 自定义response result,用于前后端分离
result = {'count': '', 'data': '', 'meta': {'msg': 'OK', 'status': 200}}
try:
# 加密分页
workorder = WorkOrderInfo.objects.all()
# count = WorkOrderInfo.objects.count()
pg = MyCursorPagination()
page_query = pg.paginate_queryset(queryset=workorder, request=request, view=self)
workorder_ser = WorkOrderSerializer(page_query, many=True)
print(workorder_ser)
# res = pg.get_paginated_response(workorder_ser)
# result['data'] = workorder_ser.data
# result['count'] = count
# print(result)
except Exception as e:
result['meta']['msg'] = '获取数据失败'
result['meta']['status'] = 500
# 注:使用 get_paginated_response 方法才能返回上下页的正确页码
return pg.get_paginated_response(workorder_ser.data)