码迷,mamicode.com
首页 > 其他好文 > 详细

djangorestframework-jwt 分页器 三种

时间:2019-05-21 13:04:20      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:field   url   har   views   数据序列化   view   from   ssi   mode   

数据准备

# 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 分页器 三种

标签:field   url   har   views   数据序列化   view   from   ssi   mode   

原文地址:https://www.cnblogs.com/tangda/p/10899075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!