djangorestframework-jwt 分页器 三种
Posted tangda
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了djangorestframework-jwt 分页器 三种相关的知识,希望对你有一定的参考价值。
数据准备
# models class Teacher(models.Model): name = models.CharField(max_length=32) salary = models.DecimalField(max_digits=5, decimal_places=2) # objson class TeacherJson(serializers.ModelSerializer): class Meta: model = models.Teacher fields = ‘__all__‘ # urls url(r‘^teachers/‘, views.Teachers.as_view()), # views class Teachers(APIView): def post(self, request): list = [] for i in range(1, 51): list.append(models.Teacher(name="%s老师" % i, salary=i)) models.Teacher.objects.bulk_create(list) return Response({ ‘status‘: 0, ‘msg‘: ‘ok‘ })
简单分页
from rest_framework.pagination import PageNumberPagination class Teachers(APIView): permission_classes = [] throttle_classes = [] throttle_classes = [] def get(self, request): # 没有分页的所有数据 teacher_list = models.Teacher.objects.all() # 完成分页 # 1) 初始化分页对象 page_simple = PageNumberPagination() # 2) 配置分页对象 # page_simple.page_size = api_settings.PAGE_SIZE # 一页显示的条数 page_simple.page_size = 5 # /teachers/?pages=3 默认page page_simple.page_query_param = ‘pages‘ # /teachers/?pages=3&line=10 用户可以自定义访问一页有多少条数据 page_simple.page_size_query_param = ‘line‘ # 限制用户自定义一页最大能访问的条数 page_simple.max_page_size = 10 # 3) 操作分页后一页的数据 teacher_page_list = page_simple.paginate_queryset(teacher_list, request, self) # 将一页的数据序列化后返回给前台 teacher_data = objson.TeacherJson(teacher_page_list, many=True).data return Response({ ‘status‘: 0, ‘msg‘: ‘ok‘, ‘results‘: teacher_data })
偏移分页
from rest_framework.pagination import LimitOffsetPagination class Teachers(APIView): def get(self, request): teacher_list = models.Teacher.objects.all() # 完成分页 # 1) 初始化分页对象 page_limit = LimitOffsetPagination() # 2) 配置分页对象 # 一页显示的条数 page_limit.default_limit = 5 # 自定义一页获取的条数 page_limit.limit_query_param = ‘limit‘ # 从哪条数据开始查询 page_limit.offset_query_param = ‘offset‘ # 自定义最大获取一页的条数 page_limit.max_limit = 8 # 3) 操作分页数据 teacher_page_list = page_limit.paginate_queryset(teacher_list, request, self) teacher_data = objson.TeacherJson(teacher_page_list, many=True).data return Response({ ‘status‘: 0, ‘msg‘: ‘ok‘, ‘previous‘: page_limit.get_previous_link(), # 上一页 ‘next‘: page_limit.get_next_link(), # 下一页 ‘results‘: teacher_data }) # return page_limit.get_paginated_response(teacher_data) # 内部提供的Response对象
游标分页
from rest_framework.pagination import CursorPagination class Teachers(APIView): def get(self, request): teacher_list = models.Teacher.objects.all() # 完成分页 # 1) 初始化分页对象 page_cursor = CursorPagination() # 2) 配置分页对象 # 一页的条数 page_cursor.page_size = 10 # 排序的字段,可以正向也可以反向 page_cursor.ordering = ‘-id‘ # 游标的关键字 page_cursor.cursor_query_param = ‘cursor‘ # 3) 操作分页数据 teacher_page_list = page_cursor.paginate_queryset(teacher_list, request, self) teacher_data = objson.TeacherJson(teacher_page_list, many=True).data return Response({ ‘status‘: 0, ‘msg‘: ‘ok‘, ‘previous‘: page_cursor.get_previous_link(), # 上一页 ‘next‘: page_cursor.get_next_link(), # 下一页 ‘results‘: teacher_data })
以上是关于djangorestframework-jwt 分页器 三种的主要内容,如果未能解决你的问题,请参考以下文章
djangorestframework-jwt 未提供身份验证凭据
djangorestframework-jwt 分页器 三种
使用 djangorestframework-jwt gain_jwt_token 视图时是不是需要创建一个简单的登录视图?