Django REST framework基礎:分頁

DRF分頁組件

爲何要使用分頁

咱們數據表中可能會有成千上萬條數據,當咱們訪問某張表的全部數據時,咱們不太可能須要一次把全部的數據都展現出來,由於數據量很大,對服務端的內存壓力比較大還有就是網絡傳輸過程當中耗時也會比較大。api

一般咱們會但願一部分一部分去請求數據,也就是咱們常說的一頁一頁獲取數據並展現出來。網絡

DRF使用分頁器

分頁模式

rest framework中提供了三種分頁模式:加密

from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination

全局配置

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'PAGE_SIZE': 100
}

局部配置

咱們能夠在視圖類中進行局部設置spa

class PublisherViewSet(ModelViewSet):
queryset = models.Publisher.objects.all()
serializer_class = PublisherModelSerializer
pagination_class = PageNumberPagination # 注意不是列表(只能有一個分頁模式)

DRF內置分頁器

PageNumberPagination

按頁碼數分頁,第n頁,每頁顯示m條數據rest

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

分頁器

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條數據xml

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

分頁器

# 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)
相關文章
相關標籤/搜索