【DRF-11】rest-framework之分页

  • 前言:总共有三种分页方式
  • 1.根据页码进行分页,看第n页,每页显示n条数据;
from rest_framework.pagination import  PageNumberPagination

class PageGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserGroup
        fields = '__all__'

class MyPageNumberPagination(PageNumberPagination):
    # page_size = api_settings.PAGE_SIZE
    # # 默认每页显示的数据条数,可全局配置
    page_size = 2
    # 获取URL参数中传入的页码key
    page_query_param = 'page'
    # 获取URL参数中设置的每页显示数据条数
    page_size_query_param = "size"
    # 最大支持的每页显示的数据条数
    max_page_size = 5


class PageGroupView(APIView):
    def get(self,request,*args,**kwargs):
        # 获取数据
        obj = models.UserGroup.objects.all()
        # ser = PageGroupSerializer(instance=obj,many=True)
        # ret = json.dumps(ser.data,ensure_ascii=False)

        # 创建分页对象
        pg = MyPageNumberPagination()
        # 在数据库中获取分页的数据
        pager_roles = pg.paginate_queryset(queryset=obj,request=request,view=self)
        # 序列化
        ser = PageGroupSerializer(instance=pager_roles,many=True)
        return HttpResponse(ser.data)

  • 2.位置和个数进行分页,在n个位置,向后查看n条数据
from rest_framework.pagination import  PageNumberPagination,LimitOffsetPagination,CursorPagination

class PageGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserGroup
        fields = '__all__'

class MyLimitOffsetPagination(LimitOffsetPagination):
    # 默认每页显示的数据条数
    default_limit = 3
    # URL中传入的显示数据条数的参数
    limit_query_param = 'limit'
    # URL中传入的数据位置的参数
    offset_query_param = 'offset'
    # 最大每页显得条数
    max_limit = 5


class PageGroupView(APIView):
    def get(self,request,*args,**kwargs):
        # 获取数据
        obj = models.UserGroup.objects.all()
        # ser = PageGroupSerializer(instance=obj,many=True)
        # ret = json.dumps(ser.data,ensure_ascii=False)

        # 创建分页对象
        # pg = MyPageNumberPagination()
        pg = MyLimitOffsetPagination()
        # 在数据库中获取分页的数据
        pager_roles = pg.paginate_queryset(queryset=obj,request=request,view=self)
        # 序列化
        ser = PageGroupSerializer(instance=pager_roles,many=True)
        # return HttpResponse(ser.data)
        return pg.get_paginated_response(ser.data)

  • 2.1:注意点:使用pg.get_paginated_response(ser.data)返回,响应中会多三个参数,总条数,上一页,下一页
"count": 10,
"next": "http://127.0.0.1:8000/api/v1/page1/?limit=3&offset=8",
"previous": "http://127.0.0.1:8000/api/v1/page1/?limit=3&offset=2",
  • 3.游标分页,加密分页,上一页和下一页。
from rest_framework.pagination import  PageNumberPagination,LimitOffsetPagination,CursorPagination

class PageGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserGroup
        fields = '__all__'

class MyCursorPagination(CursorPagination):
    # URL传入的游标参数
    cursor_query_param = 'cursor'
    # 默认每页显示的数据条数
    page_size = 2
    # URL传入的每页显示条数的参数
    page_size_query_param = None
    # 每页显示数据最大条数
    max_page_size = None
    # 根据ID从大到小排列
    ordering = "id"

class PageGroupView(APIView):
    def get(self,request,*args,**kwargs):
        # 获取数据
        obj = models.UserGroup.objects.all()
        # ser = PageGroupSerializer(instance=obj,many=True)
        # ret = json.dumps(ser.data,ensure_ascii=False)

        # 创建分页对象
        # pg = MyPageNumberPagination()
        # pg = MyLimitOffsetPagination()
        pg = MyCursorPagination()
        # 在数据库中获取分页的数据
        pager_roles = pg.paginate_queryset(queryset=obj,request=request,view=self)
        # 序列化
        ser = PageGroupSerializer(instance=pager_roles,many=True)
        # return HttpResponse(ser.data)
        return pg.get_paginated_response(ser.data)

posted @ 2024-06-04 21:11  Tony_xiao  阅读(13)  评论(0编辑  收藏  举报