分页

作者: 马小跳_ | 来源:发表于2018-02-08 15:18 被阅读54次

    解决大数据分页的效率问题:

    • 记录当前访问页的数据ID,在发送下一页的访问请求时,SQL语句中进行约束(id>page_size*page_num)
    • 最多显示120页(参考抽屉新热榜)
    • 只显示上一页下一页按钮,对页码加密

    django-restframework中有三种分页类:

    • LimitOffsetPagination——基于offest+limit

    • PageNumberPagination——基于页码

    • CursorPagination——基于cursor

    class LimitOffsetPagination(BasePagination):
        default_limit = api_settings.PAGE_SIZE  # 默认每页显示多少条
        limit_query_param = 'limit'  # 获取请求中每页显示条数时的key
        offset_query_param = 'offset'  # 偏移量的key
        max_limit = None  # 客户端每页最大请求条数
    
    class PageNumberPagination(BasePagination):
        page_size = api_settings.PAGE_SIZE  # 默认每页显示多少条,默认为None,表示禁用分页
        page_query_param = 'page'  # 获取页数时的key
        page_size_query_param = None  # 客户端请求时,每页条数的key,可以设置为'page_size'
        max_page_size = None  # 客户端每页最大请求条数
        last_page_strings = ('last',)
        
        
    class CursorPagination(BasePagination):
        cursor_query_param = 'cursor'
        page_size = api_settings.PAGE_SIZE  # 每页几条
        ordering = '-created'  # 排序
        page_size_query_param = None  # 客户端请求时,每页条数的key,可以设置为'page_size'
        max_page_size = None  # 最大请求到多少页
        offset_cutoff = 1000
    

    使用

    1.根据页码进行分页

    urls.py

    from django.conf.urls import url, include
    from rest_framework import routers
    from web.views import s9_pagination
    
    urlpatterns = [
        url(r'^test/', s9_pagination.UserViewSet.as_view()),
    ]
    

    views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework import serializers
    from .. import models
    
    from rest_framework.pagination import PageNumberPagination
    
    
    class StandardResultsSetPagination(PageNumberPagination):
        # 默认每页显示的数据条数
        page_size = 1
        # 获取URL参数中设置的每页显示数据条数
        page_size_query_param = 'page_size'
    
        # 获取URL参数中传入的页码key
        page_query_param = 'page'
    
        # 最大支持的每页显示的数据条数
        max_page_size = 1
    
    
    class UserSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.UserInfo
            fields = "__all__"
    
    
    class UserViewSet(APIView):
        def get(self, request, *args, **kwargs):
            user_list = models.UserInfo.objects.all().order_by('-id')
    
            # 实例化分页对象,获取数据库中的分页数据
            paginator = StandardResultsSetPagination()
            page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)
    
            # 序列化对象
            serializer = UserSerializer(page_user_list, many=True)
    
            # 生成分页和数据
            response = paginator.get_paginated_response(serializer.data)
            return response
    

    2.根据位置和个数进行分页

    urls.py

    from django.conf.urls import url, include
    from web.views import s9_pagination
    
    urlpatterns = [
        url(r'^test/', s9_pagination.UserViewSet.as_view()),
    ]
    

    views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework import serializers
    from .. import models
    
    from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination
    
    
    class StandardResultsSetPagination(LimitOffsetPagination):
        # 默认每页显示的数据条数
        default_limit = 10
        # URL中传入的显示数据条数的参数
        limit_query_param = 'limit'
        # URL中传入的数据位置的参数
        offset_query_param = 'offset'
        # 最大每页显得条数
        max_limit = None
    
    class UserSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.UserInfo
            fields = "__all__"
    
    
    class UserViewSet(APIView):
        def get(self, request, *args, **kwargs):
            user_list = models.UserInfo.objects.all().order_by('-id')
    
            # 实例化分页对象,获取数据库中的分页数据
            paginator = StandardResultsSetPagination()
            page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)
    
            # 序列化对象
            serializer = UserSerializer(page_user_list, many=True)
    
            # 生成分页和数据
            response = paginator.get_paginated_response(serializer.data)
            return response
    

    3.游标分页

    urls.py

    from django.conf.urls import url, include
    from web.views import s9_pagination
    
    urlpatterns = [
        url(r'^test/', s9_pagination.UserViewSet.as_view()),
    ]
    

    views.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from rest_framework.views import APIView
    from rest_framework import serializers
    from .. import models
    
    from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
    
    
    class StandardResultsSetPagination(CursorPagination):
        # URL传入的游标参数
        cursor_query_param = 'cursor'
        # 默认每页显示的数据条数
        page_size = 2
        # URL传入的每页显示条数的参数
        page_size_query_param = 'page_size'
        # 每页显示数据最大条数
        max_page_size = 1000
    
        # 根据ID从大到小排列
        ordering = "id"
    
    
    
    class UserSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.UserInfo
            fields = "__all__"
    
    
    class UserViewSet(APIView):
        def get(self, request, *args, **kwargs):
            user_list = models.UserInfo.objects.all().order_by('-id')
    
            # 实例化分页对象,获取数据库中的分页数据
            paginator = StandardResultsSetPagination()
            page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)
    
            # 序列化对象
            serializer = UserSerializer(page_user_list, many=True)
    
            # 生成分页和数据
            response = paginator.get_paginated_response(serializer.data)
            return response
    

    相关文章

      网友评论

          本文标题:分页

          本文链接:https://www.haomeiwen.com/subject/iyyttftx.html