Django框架(二十六)—— Django rest_framework-分页器与版本控制
分页器与版本控制
一、三种分页器
普通分页
偏移分页
加密分页
二、分页器
# 路由
url(r'^books/', views.Book.as_view({'get':'get_all'}))
1、普通分页(PageNumberPagination)
http://127.0.0.1:8000/books/?aa=2&size=4
from rest_framework.pagination import PageNumberPagination
class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查询成功'}
book_list = models.Book.objects.all()
# 实例化产生一个分页对象
# 不继承来修改对象的值
page=PageNumberPagination()
page.page_size=2 # 每页显示的个数
page.page_query_param='pag' # 路由中?后面的key,指定页码
page.page_size_query_param = 'size' # 指定当前页显示多少条
page.max_page_size = 5 # 每页最多显示多少条
# 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的时候,用分页之后的数据
ser = mySer.BookSerializer(instance=page_list, many=True)
# 会带着链接,和总共的条数(不建议用,会把总数据条数返回)
# return page.get_paginated_response(ser.data)
return Response(ser.data)
REST_FRAMEWORK = {
# 每页显示两条
'PAGE_SIZE':2
}
结果:
2、偏移分页(LimitOffsetPagination)
http://127.0.0.1:8000/books/?offset=2&limit=2
from rest_framework.pagination import LimitOffsetPagination
class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查询成功'}
book_list = models.Book.objects.all()
# 实例化产生一个偏移分页对象
page=LimitOffsetPagination()
page.default_limit = 3 # 每页显示的条数
page.offset_query_param = 'offset' # 从哪一页开始的标杆的key,如 offset=3
page.limit_query_param = 'limit' # 往后偏移多少的key值,如 limit=4
page.max_limit = 5 # 每页显示最大的条数
page_list = page.paginate_queryset(book_list, request, self)
ser = mySer.BookSerializer(instance=page_list, many=True)
return Response(ser.data)
结果:
3、加密分页(CursorPagination)
http://127.0.0.1:8000/books/?cursor=cj0xJnA9MTA%3D
from rest_framework.pagination import CursorPagination
class Book(ViewSetMixin, APIView):
def get_all(self, request):
book_list = models.Book.objects.all()
# 实例化产生一个加密分页对象
page = CursorPagination()
page.page_size = 3 # 每页显示多少条
page.ordering = 'nid' # 按nid排序
page.cursor_query_param = 'cursor' # 查询的key值
page_list = page.paginate_queryset(book_list, request, self)
ser = mySer.BookSerializer(instance=page_list, many=True)
return page.get_paginated_response(ser.data)
结果:
4、警告处理
(1)可能会报如下的警告,对于无序的数据,分页器生成的分页数据可能不一致:
UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'api.models.Course'> QuerySet.
paginator = self.django_paginator_class(queryset, page_size)
因此在获取数据库数据时,可以做一下排序,这样就不会报警告了:
book_list = models.Book.objects.all().order_by('pk')
(2)可能出现以下警告,因为在配置过PAGE_ZIGE,没有设置DEFAULT_PAGINATION_CLASS默认为None
(rest_framework.W001) You have specified a default PAGE_SIZE pagination rest_framework setting,without specifying also a DEFAULT_PAGINATION_CLASS.
HINT: The default for DEFAULT_PAGINATION_CLASS is None. In previous versions this was PageNumberPagination. If you wish to define PAGE_SIZE globally whilst defining pagination_class on a per-view basis you may silence this check.
因此,只需在配置文件中设置DEFAULT_PAGINATION_CLASS
REST_FRAMEWORK = {
'PAGE_SIZE': 2,
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
}
三、版本控制
1、使用
# 路由层
url(r'^(?P<version>v[123]+)/books/$', views.Book.as_view({'get':'get_all'})),
# settings
REST_FRAMEWORK = {
# 'DEFAULT_VERSIONING_CLASS':'', # 全局配置版本控制类
'VERSION_PARAM':'version',
'DEFAULT_VERSION':'v1',
'ALLOWED_VERSIONS': ['v1', 'v2'],
}
# 视图层
from rest_framework.versioning import URLPathVersioning, QueryParameterVersioning, AcceptHeaderVersioning
# QueryParameterVersioning, AcceptHeaderVersioning 不常用
class Book(ViewSetMixin, APIView):
# 重定义 versioning_class
versioning_class = URLPathVersioning
def get_all(self, request,*args,**kwargs):
# 可以从request中获取版本号
print(request.version)
book_list = models.Book.objects.all()
# 实例化产生一个加密分页对象
page = CursorPagination()
page.ordering = 'nid'
page_list = page.paginate_queryset(book_list, request, self)
ser = mySer.BookSerializer(instance=page_list, many=True)
return page.get_paginated_response(ser.data)
2、反向解析
# 路由层
url(r'^(?P<version>v[123]+)/books/$', views.Book.as_view({'get':'get_all'},name='ttt')),
# 1.反向解析获取路由
from django.urls import reverse
url2=reverse(viewname='ttt',kwargs={'version':request.version})
print(url2)
# 2.利用提供的解析
url2=request.versioning_scheme.reverse('ttt',request=request)
print(url2)
class Book(ViewSetMixin, APIView):
versioning_class = URLPathVersioning
def get_all(self, request,*args,**kwargs):
# 从request中获取版本号
print(request.version)
book_list = models.Book.objects.all()
# 实例化产生一个加密分页对象
page = CursorPagination()
page.ordering = 'nid'
page_list = page.paginate_queryset(book_list, request, self)
ser = mySer.BookSerializer(instance=page_list, many=True)
return page.get_paginated_response(ser.data)
博客内容仅供参考,部分参考他人优秀博文,仅供学习使用