一 REST framwork分页器:
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination class PNPagination(PageNumberPagination):
page_size = 1
page_query_param = 'page'
page_size_query_param = "size"
max_page_size = 5 class BookViewSet(viewsets.ModelViewSet): queryset = Book.objects.all()
serializer_class = BookSerializers
def list(self,request,*args,**kwargs): book_list=Book.objects.all()
pp=LimitOffsetPagination()
pager_books=pp.paginate_queryset(queryset=book_list,request=request,view=self)
print(pager_books)
bs=BookSerializers(pager_books,many=True) #return Response(bs.data)
return pp.get_paginated_response(bs.data)
二 路由器:
在view视图中我们已经将代码进行最大化的简化,但是有一个问题就是我们的urls 中不得不进行传参,以区别不同的GET请求,分别为:Retrieve与List
url(r'^authors/$', views.AuthorModelView.as_view({"get": "list", "post": "create"})),
url(r'^authors/(?P<pk>\d+)/$',views.AuthorModelView.as_view({"get": "retrieve", "put": "update", "delete": "destroy"})),
使用了REST framwork路由器:
三 响应器: