一:自定义配置settings文件
1、有两套配置文件,默认配置,用户的配置
2、如果某个字段,用户配置了,就用用户的,如果没配置,就用默认的
二:分页器
1、三种分页:
# 普通分页 from rest_framework.pagination import PageNumberPagination # 偏移分页 from rest_framework.pagination import LimitOffsetPagination # 加密分页 from rest_framework.pagination import CursorPagination
class MyPageNumberPagination(PageNumberPagination): # 每页显示多少条 page_size = 3 # 查询的key,问号后面的key page_query_param = 'aa' # 指定当前页显示多少条 page_size_query_param='size' # 每页最多显示多少条 max_page_size=4
(1)第一种:类似于原来django中的普通分页
# 每页显示多少条 page_size = api_settings.PAGE_SIZE # 查询的key,问号后面的key page_query_param = 'page' # 指定当前页显示多少条 page_size_query_param # 每页最多显示多少条 max_page_size
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='bb' # page = MyPageNumberPagination() # 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象 page_list = page.paginate_queryset(book_list, request, self) # 再序列化的时候,用分页之后的数据 ser = mySer.BookSerializer(instance=page_list, many=True) # response['data'] = ser.data # return Response(response) # 会带着链接,和总共的条数(不建议用) return page.get_paginated_response(ser.data) # return Response(ser.data)
(2)第二种:偏移分页
#每页显示的条数 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): response = {'status': 100, 'msg': '查询成功'} book_list = models.Book.objects.all() # 实例化产生一个偏移分页对象 page=LimitOffsetPagination() page.default_limit=3 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)第三种:加密分页(查询速度快)
#按nid排序 page.ordering = 'nid' #查询的key值 cursor_query_param = 'cursor' #每页显示多少条 page_size = api_settings.PAGE_SIZE
from rest_framework.pagination import CursorPagination class Book(ViewSetMixin, APIView): def get_all(self, request): response = {'status': 100, 'msg': '查询成功'} 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、使用:
(1)生成一个对象(普通,偏移,加密)
(2)***重点:可以修改对象的属性
# ret=对象. 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象 paginate_queryset(book_list, request, self)
(3)丢到序列化类中,完成序列化,
(4)最后返回
三:版本控制
1、掌握的:URLPathVersioning
from rest_framework.versioning import URLPathVersioning, # 了解 # QueryParameterVersioning, # AcceptHeaderVersioning
2、在setting中配置:
REST_FRAMEWORK = { # 每页显示两条 'PAGE_SIZE':2, # 'DEFAULT_VERSIONING_CLASS':'', 'VERSION_PARAM':'version', 'DEFAULT_VERSION':'v1', 'ALLOWED_VERSIONS': ['v1', 'v2'], }
3、局部使用:
versioning_class = URLPathVersioning
4、全局使用:
'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning',
5、settings路由配置:
url(r'^(?P<version>[v1|v2|v3]+)/books/$', views.Book.as_view({'get':'get_all'}),name='ttt'), url(r'^books/(?P<pk>d+)', views.Book.as_view({'get':'get_one'})),
6、在视图类中的request对象中,就可以取出版本号
7、反向解析:
url2=request.versioning_scheme.reverse('ttt',request=request)
class Book(ViewSetMixin, APIView): versioning_class = URLPathVersioning def get_all(self, request,*args,**kwargs): print(request.version) # 路由反向解析 # from django.urls import reverse # url2=reverse(viewname='ttt',kwargs={'version':request.version}) # print(url2) # 提供给咱的解析 url2=request.versioning_scheme.reverse('ttt',request=request) print(url2) response = {'status': 100, 'msg': '查询成功'} 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)
from rest_framework.versioning import URLPathVersioning, QueryParameterVersioning, AcceptHeaderVersio