分页器
PageNumberPagination(普通分页器)
说明:
配置分页器的中最大显示个数(也可在setting中做全局配置)
page.page_size = 2
REST_FRAMEWORK = { # 每页显示两条 'PAGE_SIZE': 2, }
配置地址栏中的地址(http://127.0.0.1:8001/books/?page=2)对应最后的page
page.page_query_param = 'page'
指定当前页显示多少条
page.page_size_query_param = 'size'
限制最大页数
page.max_page_size = 6
class Book(ViewSetMixin, APIView): def get_all(self, request): db_books = models.Books.objects.all() page = PageNumberPagination() page.page_size = 2 page.page_query_param = 'page' page.page_size_query_param = 'size' page.max_page_size = 6 book_page = page.paginate_queryset(db_books, request, view=self) books_ser = myser.BookSerializers(book_page, many=True) return Response(books_ser.data)
LimitOffsetPagination(偏移分页器)
说明:
每页显示的条数
default_limit = api_settings.PAGE_SIZE
标杆值
offset_query_param = 'offset'
往后偏移多少
limit_query_param = 'limit'
每页显示最大的条数
max_limit = None
注:要取的条数,如果超过了,还剩多少,就显示多少
from rest_framework.pagination import LimitOffsetPagination class Book(ViewSetMixin, APIView): def get_all(self, request): db_books = models.Books.objects.all() page = LimitOffsetPagination() page.default_limit = 3 page.offset_query_param = 'offset' page.max_limit = 3 book_page = page.paginate_queryset(db_books, request, view=self) books_ser = myser.BookSerializers(book_page, many=True) return Response(books_ser.data)
CursorPagination(加密分页器-查询速度很快)
说明:
按id排序
page.ordering = 'nid'
查询的key值
cursor_query_param = 'cursor'
每页显示多少条
page_size = api_setting.PAGE_SIZE
class Book(ViewSetMixin, APIView): def get_all(self, request): db_books = models.Books.objects.all() page = CursorPagination() # 设置根据数据库中哪个字段排序 page.ordering = 'id' book_page = page.paginate_queryset(db_books, request, view=self) books_ser = myser.BookSerializers(book_page, many=True) # return Response(books_ser.data) return page.get_paginated_response(books_ser.data)
使用总结:
生成一个对象普通,偏移,加密)
***重点:可以修改对象的属性
-ret = 对象.paginate_queryset(book_list,request,self)
-丢到序列化类中,完成序列化
-最后返回
版本控制
基于url的正则方式:URLPathVersioning(/v1/users/)
基于url的get传参方式:QueryParameterVersioning(users?version=v1)
基于 accept 请求头方式:AcceptHeaderVersioning(Accept: application/json; version=1.0)
局部使用:
versioning_class = URLPathVersioning
全局使用:
REST_FRAMEWORK = { 'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.QueryParameterVersioning', 'DEFAULT_VERSION': 'v1', # 默认版本(从request对象里取不到,显示的默认值) 'ALLOWED_VERSIONS': ['v1', 'v2'], # 允许的版本 'VERSION_PARAM': 'version' # URL中获取值的key }
URLPathVersioning示例
路由配置:
urlpatterns = [ url(r'^(?P<version>[v1|v2]+)/books/', views.Book.as_view({'get': 'get_all'}), name='ttt'), ]
视图函数:
from rest_framework.versioning import URLPathVersioning class Book(ViewSetMixin, APIView): versioning_class = URLPathVersioning def get_all(self, request, version): db_books = models.Books.objects.all() print(request.version) reverse_url = request.versioning_scheme.reverse('ttt', request=request) print(reverse_url) page = PageNumberPagination() page.max_page_size = 6 book_page = page.paginate_queryset(db_books, request, view=self) books_ser = myser.BookSerializers(book_page, many=True) return page.get_paginated_response(books_ser.data)