<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          drf分頁器的使用

          共 6637字,需瀏覽 14分鐘

           ·

          2021-03-26 14:33

          目錄

          • 1、概述

          • 2、PageNumberPagination

          • 3、LimitOffsetPagination

          • 4、CursorPagination

          1、概述

          Django REST framework提供了分頁的支持

          可以在配置文件中設(shè)置全局的分頁方式,如:

          REST_FRAMEWORK = {
              'DEFAULT_PAGINATION_CLASS':  'rest_framework.pagination.PageNumberPagination'# 選用的分頁器
              'PAGE_SIZE'100  # 每頁數(shù)目
          }

          也可通過自定義Pagination類,來為視圖添加不同分頁行為。在視圖中通過pagination_clas屬性來指明

          例如:

          from rest_framework.pagination import PageNumberPagination

          class MyPageNumberPagination(PageNumberPagination):
              # 設(shè)置url中的取多少頁的key
              page_query_param = 'page'
              # 設(shè)置url中設(shè)置取數(shù)據(jù)條數(shù)的key
              page_size_query_param = 'size'
              #設(shè)置每一頁的數(shù)據(jù)條數(shù)
              page_size = 2
              # 設(shè)置每一頁最多可取的數(shù)據(jù)數(shù)
              max_page_size = 5
             
          class Book2View(ListAPIView):
              queryset = models.Book.objects.all()
              serializer_class = BookModelSerializer
              pagination_class = MyPageNumberPagination #指定該視圖類的分頁器

          注意:如果在視圖內(nèi)關(guān)閉分頁功能,只需在視圖內(nèi)設(shè)置pagination_class = None 即可,即在設(shè)置了全局分頁之后我們可以在局部禁用該設(shè)置

          drf 提供給我們的分頁器有PageNumberPagination、LimitOffsetPagination、CursorPagination

          2、PageNumberPagination

          url形式:

          http://127.0.0.1:8000/books/?page=1

          可以在自定義類中定義的屬性有:

          • page_query_param :設(shè)置url中頁數(shù)的關(guān)鍵字,默認(rèn)的是page
          • page_size_query_param :設(shè)置url中每頁數(shù)據(jù)條數(shù)的關(guān)鍵字,默認(rèn)的是None
          • page_size :設(shè)置每一頁的數(shù)據(jù)條數(shù)(必設(shè))
          • max_page_size :設(shè)置每一頁最多可取的數(shù)據(jù)條數(shù)(可選)
          class MyPageNumberPagination(PageNumberPagination):
              page_size = 2
              page_query_param = 'page'
              page_size_query_param = 'size'
              max_page_size = 5
              
          class Book2View(ListAPIView):
              queryset = models.Book.objects.all()
              serializer_class = BookModelSerializer
              pagination_class = MyPageNumberPagination
             
          # APIView 中使用自定義分頁類進(jìn)行分頁操作
          class  Pager(APIView):
              def get(self,request,*args,**kwargs):
                  # 獲取所有數(shù)據(jù)
                  ret=models.Book.objects.all()
                  # 創(chuàng)建分頁對象
                  page=MyPageNumberPagination()
                  # 在數(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)
                  # 這個(gè)也是返回Response對象,但是比基本的多了上一頁,下一頁,和總數(shù)據(jù)條數(shù)(了解)
                  return page.get_paginated_response(ser.data)

          3、LimitOffsetPagination

          url形式:

          http://127.0.0.1/four/books/?limit=100&offset=400

          可以在自定義類中定義的屬性有:

          • default_limit  :默認(rèn)限制,默認(rèn)值與PAGE_SIZE設(shè)置一致
          • limit_query_paramlimit參數(shù)名,默認(rèn)為limit
          • offset_query_paramoffset參數(shù)名,默認(rèn)offset
          • max_limit :最大limit限制,默認(rèn)None
          class MyLimitOffsetPagination(LimitOffsetPagination):
              default_limit = 3
              limit_query_param = 'limit'
              max_limit = None
              offset_query_param = 'offset'
              
          class Book2View(ListAPIView):
              queryset = models.Book.objects.all()
              serializer_class = BookModelSerializer
              pagination_class = MyLimitOffsetPagination
              
          # APIView 中使用自定義分頁類進(jìn)行分頁操作
          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)

          4、CursorPagination

          url形式:

          http://127.0.0.1/four/books/?cursor=cD0xNQ%3D%3D

          可以在自定義類中定義的屬性有:

          • cursor_query_param:默認(rèn)查詢字段,不需要修改
          • page_size:每頁數(shù)目
          • ordering:按什么排序,需要指定
          class MyCursorPagination(CursorPagination):
              cursor_query_param = 'cursor'
              page_size = 2
              ordering = '-id'

          class Book2View(ListAPIView):
              queryset = models.Book.objects.all()
              serializer_class = BookModelSerializer
              pagination_class = MyCursorPagination
              
          # APIView 中使用自定義分頁類進(jìn)行分頁操作
          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)
          瀏覽 124
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  2025国产精品 | 超碰人人妻 | 乱老熟女一区二区三区 | 日本a在线免费观看 | 黄色录像视频大片 |