我們數(shù)據(jù)表中可能會有成千上萬條數(shù)據(jù),當我們訪問某張表的所有數(shù)據(jù)時,我們不太可能需要一次把所有的數(shù)據(jù)都展示出來,因為數(shù)據(jù)量很大,對服務端的內(nèi)存壓力比較大還有就是網(wǎng)絡傳輸過程中耗時也會比較大。
創(chuàng)新互聯(lián)是一家專業(yè)提供宜陽企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站設計、做網(wǎng)站、H5技術、小程序制作等業(yè)務。10年已為宜陽眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
通常我們會希望一部分一部分去請求數(shù)據(jù),也就是我們常說的一頁一頁獲取數(shù)據(jù)并展示出來。
model.py
from django.db import models
# Create your models here.
from django.db import models
class Book(models.Model):
name = models.CharField(max_length=32)
price = models.CharField(max_length=32)
publish = models.CharField(max_length=32)
def __str__(self):
return self.name
serializer.py
from rest_framework import serializers
from .models import Book, Publish
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
page.py
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
class CommonPageNumberPagination(PageNumberPagination):
# 有4個類屬性
# 每頁顯示條數(shù)
page_size = 2
# 分頁查詢的那個參數(shù) ?page=10
page_query_param = 'page'
# ?page=3&size=3 查詢3頁,每頁查詢3條
page_size_query_param = 'size'
# 可以通過size控制每頁顯示的條數(shù),但是通過這個參數(shù)控制最多顯示多少條
max_page_size = 3
view.py
from django.shortcuts import render
# Create your views here.
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin
from rest_framework.viewsets import ViewSetMixin
from app01.models import Book
from app01.serializer import BookSerializer
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
# as 后面是起的別名,將我們寫的類導入進來
from .page import CommonPageNumberPagination as PageNumberPagination
class BookView(ViewSetMixin, GenericAPIView, ListModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
# pagination_class后面是我們自己寫的類,只不過在導入的時候我們重新命名了
pagination_class = PageNumberPagination
page.py
class CommonLimitOffsetPagination(LimitOffsetPagination):
# 每頁顯示多少條
default_limit = 2
# 可以直接從第幾頁第幾個位置開始拿數(shù)據(jù) offset=6&limit=2
limit_query_param = 'limit' # 取多少條
# 從第0個位置偏移多少開始取數(shù)據(jù)
offset_query_param = 'offset'
# 最大限制條數(shù)
max_limit = 5
view.py
from .page import CommonLimitOffsetPagination
class BookView(ViewSetMixin, GenericAPIView, ListModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
pagination_class = CommonLimitOffsetPagination
效率高,但是可控性差,只能選擇上一頁與下一頁,不能直接跳轉(zhuǎn)到某一頁,這種針對于大數(shù)據(jù)
page.py
class CommonCursorPagination(CursorPagination):
# 查詢的名字
cursor_query_param = 'cursor'
# 每頁顯示多少條
page_size = 3
# 必須是表中有的字段,一般用id
ordering = 'id'
view.py
from .page import CommonCursorPagination
class BookView(ViewSetMixin, GenericAPIView, ListModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
pagination_class = CommonCursorPagination
# 如果使用APIView分頁
class BookView(APIView):
def get(self,request,*args,**kwargs):
book_list=models.Book.objects.all()
# 1.實例化得到一個分頁器對象,MyPageNumberPagination這個是我們自己寫好的分頁器類
page_cursor=MyPageNumberPagination()
# 2.使用分頁器對象的paginate_queryset方法,該方法內(nèi)傳入queryset、request和當前視圖類參數(shù),注意返回值其實是一個新的queryset,替代了book_list
book_list=page_cursor.paginate_queryset(book_list,request,view=self)
# 3.分頁器對象的get_next_link和get_previous_link是取到下一頁和上一頁的url,然后可以返回給前端(就是將next_url放在下面的Response里返回,前端頁面就可以顯示出來上下頁的url了
next_url =page_cursor.get_next_link()
pr_url=page_cursor.get_previous_link()
# print(next_url)
# print(pr_url)
book_ser=BookModelSerializer(book_list,many=True)
return Response(data=book_ser.data)