drf提供了三個內(nèi)置分頁器,根據(jù)前端需求選擇使用。
全局配置
在配置文件中設(shè)置全局的分頁方式,如:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100 # 每頁數(shù)目
}
也可通過繼承內(nèi)置的分頁器類自定義Pagination類,來為視圖添加不同分頁器。在視圖中通過pagination_clas
屬性來指明。
class LargeResultsSetPagination(PageNumberPagination):
page_size = 10
page_query_param = 'page'
page_size_query_param = 'size'
max_page_size = 30
class BookDetailView(RetrieveAPIView):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
pagination_class = LargeResultsSetPagination # 局部指定使用分頁器
注意:視圖內(nèi)局部設(shè)置不使用分頁器
pagination_class = None
內(nèi)置分頁器的使用有兩種方式,
- 一種是直接使用內(nèi)置的,在全局配置分頁器的屬性參數(shù),控制分頁的效果。
- 一種是繼承內(nèi)置的分頁器類,在繼承的子類中設(shè)置分頁器的屬性參數(shù),控制分頁效果。
上述配置方式適用于內(nèi)置的即繼承內(nèi)置的所有分頁器的使用。需要注意的是基于APIView
的視圖需要自己手動添加分頁器對象;基于ListAPIView
的視圖(繼承GenericAPIView和視圖插件的視圖類以及視圖集合類都可使用)僅通過參數(shù)pagination_class
配置分頁器即可使用。
PageNumberPagination
前端訪問網(wǎng)址形式:文章來源:http://www.zghlxwxcb.cn/news/detail-437420.html
GET http://127.0.0.1:8000/students/?page=4
可以在子類中定義的屬性:
- page_size 每頁數(shù)目
- page_query_param 前端發(fā)送的頁數(shù)關(guān)鍵字名,默認(rèn)為”page”
- page_size_query_param 前端發(fā)送的每頁數(shù)目關(guān)鍵字名,默認(rèn)為None
- max_page_size 前端最多能設(shè)置的每頁數(shù)量
# APIView
from rest_framework.pagination import PageNumberPagination
# 一 基本使用:url=url=http://127.0.0.1:8000/pager/?page=2&size=3,size無效
class Pager(APIView):
def get(self,request,*args,**kwargs):
# 獲取所有數(shù)據(jù)
ret=models.Book.objects.all()
# 創(chuàng)建分頁對象
page=PageNumberPagination()
# 在數(shù)據(jù)庫中獲取分頁的數(shù)據(jù)
page_list=page.paginate_queryset(ret,request,view=self)
# 對分頁進(jìn)行序列化
ser=BookSerializer1(instance=page_list,many=True)
return Response(ser.data)
# 二 自定制分頁器類, url=http://127.0.0.1:8000/pager/?page=2&size=3
# size=30,無效,最多5條
class Mypage(PageNumberPagination):
page_size = 2
page_query_param = 'page'
page_size_query_param = 'size' # 定制傳參
max_page_size = 5 # 每頁最大的數(shù)據(jù)量
class Pager(APIView):
def get(self,request,*args,**kwargs):
ret=models.Book.objects.all()
page=Mypage() # 創(chuàng)建分頁對象
page_list=page.paginate_queryset(ret,request,view=self) # 在數(shù)據(jù)庫中獲取分頁的數(shù)據(jù)
ser=BookSerializer1(instance=page_list,many=True) # 對分頁進(jìn)行序列化
# return Response(ser.data)
# 這個也是返回Response對象,但是比基本的多了上一頁,下一頁,和總數(shù)據(jù)條數(shù)(了解即可)
return page.get_paginated_response(ser.data)
# ListAPIView
# 聲明分頁的配置類,即可使用
from rest_framework.pagination import PageNumberPagination
class StandardPageNumberPagination(PageNumberPagination):
page_size = 2
page_size_query_param = "size"
max_page_size = 10
page_query_param = "p"
class StudentAPIView(ListAPIView):
queryset = Student.objects.all()
serializer_class = StudentModelSerializer
pagination_class = StandardPageNumberPagination
# 127.0.0.1/four/students/?p=1&size=5
LimitOffsetPagination
前端訪問網(wǎng)址形式:
GET http://127.0.0.1/four/students/?limit=100&offset=400
可以在子類中定義的屬性:
- default_limit 默認(rèn)限制,默認(rèn)值與
PAGE_SIZE
設(shè)置一直 - limit_query_param limit參數(shù)名,默認(rèn)’limit’
- offset_query_param offset參數(shù)名,默認(rèn)’offset’
- max_limit 最大limit限制,默認(rèn)None
# APIView
# http://127.0.0.1:8000/pager/?offset=4&limit=3
from rest_framework.pagination import LimitOffsetPagination
# 也可以自定制,同簡單分頁
class Pager(APIView):
def get(self,request,*args,**kwargs):
# 獲取所有數(shù)據(jù)
ret=models.Book.objects.all()
# 創(chuàng)建分頁對象
page=LimitOffsetPagination()
# 在數(shù)據(jù)庫中獲取分頁的數(shù)據(jù)
page_list=page.paginate_queryset(ret,request,view=self)
# 對分頁進(jìn)行序列化
ser=BookSerializer1(instance=page_list,many=True)
# return page.get_paginated_response(ser.data)
return Response(ser.data)
#ListAPIView
from rest_framework.pagination import LimitOffsetPagination
class StandardLimitOffsetPagination(LimitOffsetPagination):
# 默認(rèn)每一頁查詢的數(shù)據(jù)量,類似上面的page_size
default_limit = 2
limit_query_param = "size"
offset_query_param = "start"
class StudentAPIView(ListAPIView):
queryset = Student.objects.all()
serializer_class = StudentModelSerializer
# 調(diào)用頁碼分頁類
# pagination_class = StandardPageNumberPagination
# 調(diào)用查詢偏移分頁類
pagination_class = StandardLimitOffsetPagination
CursorPagination
這種分頁方式只有上一頁和下一頁,不能跳躍切換頁面,但是查詢效率極高。
前端訪問網(wǎng)址形式:
GET http://127.0.0.1/four/students/?cursor=cD0xNQ%3D%3D
可以在子類中定義的屬性:文章來源地址http://www.zghlxwxcb.cn/news/detail-437420.html
- cursor_query_param:默認(rèn)查詢字段,不需要修改
- page_size:每頁數(shù)目
- ordering:按什么排序,需要指定,逆序在字段前加負(fù)號:
ordering='-id'
#APIView
from rest_framework.pagination import CursorPagination
# 看源碼,是通過sql查詢,大于id和小于id
class Pager(APIView):
def get(self,request,*args,**kwargs):
# 獲取所有數(shù)據(jù)
ret=models.Book.objects.all()
# 創(chuàng)建分頁對象
page=CursorPagination()
page.ordering='nid'
# 在數(shù)據(jù)庫中獲取分頁的數(shù)據(jù)
page_list=page.paginate_queryset(ret,request,view=self)
# 對分頁進(jìn)行序列化
ser=BookSerializer1(instance=page_list,many=True)
# 可以避免頁碼被猜到
return page.get_paginated_response(ser.data)
# ListAPIView
class MyCursorPagination(CursorPagination):
page_size=2
ordering='-id'
from rest_framework.generics import ListAPIView
class AuthorListView(ListAPIView):
serializer_class = serializers.AuthorModelSerializer
queryset = models.Author.objects.filter(is_delete=False)
pagination_class =MyCursorPagination
到了這里,關(guān)于DRF 分頁器的使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!