Django - DRF - 分頁器
阿新 • • 發佈:2018-12-29
目錄
1-2 自定義傳統分頁類 - 重寫 PageNumberPagination內屬性
一、簡單分頁 - 檢視第n頁,每頁顯示m條
注意:
- 必須配置每頁顯示條數:page.page_size
- page.page_query_param:配置get提交資料名
- 序列化需傳入分頁後資料物件
- return page.get_paginated_response(ser.data) - 返回資料帶著上下連結和資料總條數
1-1 基本使用
# DRF 傳統分頁 from rest_framework.pagination import PageNumberPagination from rest_framework.views import APIView from rest_framework.response import Response from app01 import MySerializers # DRF 基本使用 - 預設配置 # http://127.0.0.1:8000/index/?page=2&size=3,size無效 class Index(APIView): def get(self, request, *arges, **kwargs): book_list = models.Book.objects.all() page = PageNumberPagination() # 每頁顯示條數 page.page_size = 2 # http://127.0.0.1:8000/index/?test=2 # page_query_param修改get提交資料名 page.page_query_param='test' # 引數:分頁物件,request物件,當前檢視物件 page_list = page.paginate_queryset(book_list, request, view=self) ser = MySerializers.BookSerializers(instance=page_list, many=True) return Response(ser.data) # return page.get_paginated_response(ser.data) # return render(request, 'drf_index.html', locals())
1-2 自定義傳統分頁類 - 重寫 PageNumberPagination內屬性
注意:
- 每頁顯示資料的優先順序:前臺提交(page_size_query_param = 'size' >page_size)
# DRF 自定義傳統分頁類,重寫屬性 class MyPageNumberPagination(PageNumberPagination): # 每頁顯示資料 page_size = 3 # http://127.0.0.1:8000/index/?data=2&size=2 # get資料提交名 page_query_param = 'data' # ?後size傳輸每頁顯示條數 page_size_query_param = 'size' # 每頁最多顯示4條,若page_size大於max_page_size則優先後者 max_page_size = 5 class Index(APIView): def get(self, request, *arges, **kwargs): book_list = models.Book.objects.all() page = MyPageNumberPagination() # 引數:分頁物件,request物件,當前檢視物件 page_list = page.paginate_queryset(book_list, request, view=self) ser = MySerializers.BookSerializers(instance=page_list, many=True) # return Response(ser.data) return page.get_paginated_response(ser.data)
二、偏移分頁 - 在n位置,向後檢視m條資料
注意:
# -----------DRF 偏移分頁------------- from rest_framework.pagination import LimitOffsetPagination # DRF 自定義偏移分頁類,重寫屬性 class MyLimitOffsetPagination(LimitOffsetPagination): default_limit = 3 max_limit = 5 # 返回資料從 (offset,limit} 左開右閉 # 資料最大值(id最大) limit_query_param = 'limit' # 資料最小值(id下線) offset_query_param = 'offset' class Index(APIView): def get(self, request, *arges, **kwargs): book_list = models.Book.objects.all() page = MyLimitOffsetPagination() # 引數:分頁物件,request物件,當前檢視物件 page_list = page.paginate_queryset(book_list, request, view=self) ser = MySerializers.BookSerializers(instance=page_list, many=True) # return Response(ser.data) return page.get_paginated_response(ser.data)
三、加密分頁 - 加密分頁,速度快,只能上下頁
注意:
from rest_framework.pagination import CursorPagination # DRF 自定義偏移分頁類,重寫屬性 class MyCursorPagination(CursorPagination): page_size = 1 page_size_query_param = 'size' max_page_size = 5 # 當前頁的加密key # "next": "http://127.0.0.1:8000/index/?cursor=cD01&size=6", cursor_query_param = 'cursor' # 排序方式 ordering = 'id' class Index(APIView): def get(self, request, *arges, **kwargs): book_list = models.Book.objects.all() page = MyCursorPagination() # 引數:分頁物件,request物件,當前檢視物件 page_list = page.paginate_queryset(book_list, request, view=self) ser = MySerializers.BookSerializers(instance=page_list, many=True) # return Response(ser.data) return page.get_paginated_response(ser.data)
四、區域性配置和全域性配置
4-1 原始碼分析
4-2 全域性配置
REST_FRAMEWORK = { # 每頁顯示兩條 'PAGE_SIZE':2, }
4-3 區域性配置