普通分頁
普通分頁類似於Django中的分頁
源碼
class PageNumberPagination(BasePagination):
"""
A simple page number based style that supports page numbers as
query parameters. For example:
http://api.example.org/accounts/?page=4
http://api.example.org/accounts/?page=4&page_size=100
"""
# The default page size.
# Defaults to `None`, meaning pagination is disabled.
page_size = api_settings.PAGE_SIZE
# DjangoPaginator就是Django中的Paginator類
django_paginator_class = DjangoPaginator
# Client can control the page using this query parameter.
page_query_param = 'page'
page_query_description = _('A page number within the paginated result set.')
# Client can control the page size using this query parameter.
# Default is 'None'. Set to eg 'page_size' to enable usage.
page_size_query_param = None
page_size_query_description = _('Number of results to return per page.')
# Set to an integer to limit the maximum page size the client may request.
# Only relevant if 'page_size_query_param' has also been set.
max_page_size = None
last_page_strings = ('last',)
template = 'rest_framework/pagination/numbers.html'
invalid_page_message = _('Invalid page.')
源碼重要的四個參數詳解
# page_size是APISettings中的默認配置,用戶配置則使用用戶配置,未配置則使用DEFAULTS配置,默認為None page_size = api_settings.PAGE_SIZE # ?之后的參數,如127.0.0.1/books/?page=3 page_query_param = 'page' # 每頁最多顯示的條數,如:127.0.0.1/books/?page=3&size=5 page_size_query_param = 'size' # 超出后每頁的最多顯示數 max_page_size = None
簡單使用
REST_FRAMEWORK = { # 每頁的顯示數量 'PAGE_SIZE': 2 }
views.py文件
from rest_framework.pagination import PageNumberPagination
class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查詢成功'}
book_list = models.Book.objects.all()
# 實例化產生一個分頁對象
page = PageNumberPagination()
# 第一個參數:要分頁的數據,第二個參數request對象,第三個參數,當前視圖對象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的時候,用分頁之后的數據
ser = mySer.BookSerializer(instance=page_list, many=True)
response['data'] = ser.data
return Response(response)
或者可以自定義類來修改數據屬性
from rest_framework.pagination import PageNumberPagination
class MyPageNumberPagination(PageNumberPagination):
page_size = 3
page_query_param = 'test'
page_size_query_param = 'size'
max_page_size = 5
class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查詢成功'}
book_list = models.Book.objects.all()
# 實例化產生一個分頁對象
page = MyPageNumberPagination()
# 第一個參數:要分頁的數據,第二個參數request對象,第三個參數,當前視圖對象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的時候,用分頁之后的數據
ser = mySer.BookSerializer(instance=page_list, many=True)
response['data'] = ser.data
return Response(response)
不繼承修改屬性
from rest_framework.pagination import PageNumberPagination class Book(ViewSetMixin, APIView): def get_all(self, request): response = {'status': 100, 'msg': '查詢成功'} book_list = models.Book.objects.all() # 實例化產生一個分頁對象 # 不繼承來修改對象的值 page = PageNumberPagination() page.page_size = 2 page.page_query_param = 'bb' # page = MyPageNumberPagination() # 第一個參數:要分頁的數據,第二個參數request對象,第三個參數,當前視圖對象 page_list = page.paginate_queryset(book_list, request, self) # 再序列化的時候,用分頁之后的數據 ser = mySer.BookSerializer(instance=page_list, many=True) response['data'] = ser.data return Response(response) # 會帶着鏈接,和總共的條數(不建議用) # return page.get_paginated_response(ser.data) # return Response(ser.data)
偏移分頁
源碼
class LimitOffsetPagination(BasePagination):
"""
A limit/offset based style. For example:
http://api.example.org/accounts/?limit=100
http://api.example.org/accounts/?offset=400&limit=100
"""
default_limit = api_settings.PAGE_SIZE
limit_query_param = 'limit'
limit_query_description = _('Number of results to return per page.')
offset_query_param = 'offset'
offset_query_description = _('The initial index from which to return the results.')
max_limit = None
template = 'rest_framework/pagination/numbers.html'
重要參數詳解
# 默認顯示數量 default_limit = api_settings.PAGE_SIZE # 標桿值,從哪開始偏移 offset_query_param = 'offset' # 根據標桿值的偏移量 limit_query_param = 'limit' # 超出后限制最多的顯示多少條 max_limit = None
其余用法跟普通分頁相同,都是實例化對象,對象調用paginate_queryset(self, queryset, request, view=None)方法,參數解釋(queryset:要分頁的數據,request:request對象,view:當前視圖對象)
加密分頁
源碼
class CursorPagination(BasePagination):
"""
The cursor pagination implementation is necessarily complex.
For an overview of the position/offset style we use, see this post:
https://cra.mr/2011/03/08/building-cursors-for-the-disqus-api
"""
cursor_query_param = 'cursor'
cursor_query_description = _('The pagination cursor value.')
page_size = api_settings.PAGE_SIZE
invalid_cursor_message = _('Invalid cursor')
ordering = '-created'
template = 'rest_framework/pagination/previous_and_next.html'
# Client can control the page size using this query parameter.
# Default is 'None'. Set to eg 'page_size' to enable usage.
page_size_query_param = None
page_size_query_description = _('Number of results to return per page.')
# Set to an integer to limit the maximum page size the client may request.
# Only relevant if 'page_size_query_param' has also been set.
max_page_size = None
# The offset in the cursor is used in situations where we have a
# nearly-unique index. (Eg millisecond precision creation timestamps)
# We guard against malicious users attempting to cause expensive database
# queries, by having a hard cap on the maximum possible size of the offset.
offset_cutoff = 1000
重要參數解釋
# 按哪個字段排序 ordering = 'id' # ?后的參數 cursor_query_param = 'cursor' # 每頁的顯示數量 page_size = api_settings.PAGE_SIZE # 每頁最多顯示的條數 page_size_query_param = 'size' # 最大顯示數 max_page_size = None
最后返回時調用對象的get_paginated_response(self, data)方法,返回一個加密后的頁數鏈接

