Django REST framework基礎:分頁


DRF分頁組件

為什么要使用分頁

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

通常我們會希望一部分一部分去請求數據,也就是我們常說的一頁一頁獲取數據並展示出來。

DRF使用分頁器

分頁模式

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

from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination

全局配置

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

局部配置

我們可以在視圖類中進行局部設置

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

DRF內置分頁器

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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM