rest framework-分页-长期维护

###############  分页组件   ###############

# 分页组件
# 
# django也有分页,rest framework也有分页,但是没有页面这个概念了,
# 这个分页是返回给前端数据太多的时候,需要分页返回,
# http://127.0.0.1:8000/books/?page=1,
# 我们需要把继承这个分页类,然后重写一些方法,
# class MyPageNumberPagination(PageNumberPagination):
# page_size =2
# page_query_param = "page"
# page_size_query_param = "size"
# #####################################
# http://127.0.0.1:8000/books/?page=1&size=1
# 默认显示两个,但是可以自己指定显示几个,
# #########################################
# http://127.0.0.1:8000/books/?limit=3
# http://127.0.0.1:8000/books/?limit=3&offset=1,这是偏移,
# #############################################
#     pagination_class = MyPageNumberPagination
# 这视图里面加上这一句,就会去找我们定义的视图类了, 
# 所以分页器的使用,就是定义一个分页的类,然后加一句,就可以了,

  

############### 第一种分页器:PageNumberPagination   ###############

按页码数分页,第n页,每页显示m条数据

例如:http://127.0.0.1:8000/api/article/?page=2&size=1

分页器:

class MyPageNumber(PageNumberPagination):
    page_size = 2  # 每页显示多少条
    page_size_query_param = 'size'  # URL中每页显示条数的参数
    page_query_param = 'page'  # URL中页码的参数
    max_page_size = None  # 最大页码数限制

视图:

class ArticleList(APIView):
    def get(self, request, *args, **kwargs):
        res = {"code": 0}
        article_list = models.Article.objects.all().order_by("id")
        # 分页
        page_obj = MyPageNumber()
        page_article = page_obj.paginate_queryset(queryset=article_list, request=request, view=self)
        ser_obj = ArticleSerializer(page_article, many=True)
        res["data"] = ser_obj.data
        return Response(res)

返回带页码链接的响应:

class ArticleList(APIView):
    def get(self, request, *args, **kwargs):
        res = {"code": 0}
        article_list = models.Article.objects.all().order_by("id")
        # 分页
        page_obj = MyPageNumber()
        page_article = page_obj.paginate_queryset(queryset=article_list, request=request, view=self)
        ser_obj = ArticleSerializer(page_article, many=True)
        res["data"] = ser_obj.data
        return page_obj.get_paginated_response(res)

 

###############  第二种分页器:LimitOffsetPagination   ###############

分页,在n位置,向后查看m条数据

例如:http://127.0.0.1:8000/api/article/?offset=2&limit=2

分页器:

# offset分页
class MyLimitOffset(LimitOffsetPagination):
    default_limit = 1
    limit_query_param = 'limit'
    offset_query_param = 'offset'
    max_limit = 999

视图:

class ArticleList(APIView):
    def get(self, request, *args, **kwargs):
        res = {"code": 0}
        article_list = models.Article.objects.all().order_by("id")
        # 分页
        page_obj = MyLimitOffset()
        page_article = page_obj.paginate_queryset(queryset=article_list, request=request, view=self)
        ser_obj = ArticleSerializer(page_article, many=True)
        res["data"] = ser_obj.data
        return page_obj.get_paginated_response(res)

 

###############  第三种分页器:CursorPagination   ###############

加密分页,把上一页和下一页的id值记住

分页器:

# 加密分页
class MyCursorPagination(CursorPagination):
    cursor_query_param = 'cursor'
    page_size = 1
    ordering = '-id'  # 重写要排序的字段

视图:

class ArticleList(APIView):
    def get(self, request, *args, **kwargs):
        res = {"code": 0}
        article_list = models.Article.objects.all().order_by("id")
        # 分页
        page_obj = MyCursorPagination()
        page_article = page_obj.paginate_queryset(queryset=article_list, request=request, view=self)
        ser_obj = ArticleSerializer(page_article, many=True)
        res["data"] = ser_obj.data
        # return Response(res)
        return page_obj.get_paginated_response(res)

 

###############  分页组件   ###############

 

posted @ 2020-02-11 07:27  技术改变命运Andy  阅读(155)  评论(0编辑  收藏  举报