分页器

视图:
		快速写出5个接口:
			-继承它ModelViewSet
			-写一个视图类,视图类中定义两个变量
			class BookView(ModelViewSet):
				queryset = Book.objects.all()
				serializer_class = BookSerializer
			-路由:(两条路由)
				url(r'^publish', views.PublishView.as_view({'get': 'list', 'post': 'create'})),
				url(r'^publish/(?P<pk>\d+)', views.PublishView.as_view({'get': 'retrieve', 'put': 'update','delete':'destroy'})),
					
			
			-第一种:最原始的那种
			-第二种:继承ViewSetMixin,它放在前面(好处,跟某个功能相关的,可以写在第一起)
				-可以自己封装一个View,继承ViewSetMixin,和APIView
				
	响应器:(一般不需要配置)
		-返回用Response
		
	url控制:
		三种:
			-最原始的
			-半自动(必须继承ViewSetMixin)
				-url(r'^books/$', views.Book.as_view({'get':'get_all'})),
			-全自动(自动生成路由,必须继承自ModelViewSet)(不建议用)
				-
	配置文件查找顺序
		先从视图类中查找--->项目配置文件(settings)----->drf默认的setting中取
		
		
重点:
	手写配置文件
		-有两套配置文件,默认配置,用户的配置
		-如果某个字段,用户配置了,就用用户的,如果没配置,就用默认的
	
	分页器
		三种分页:
			第一种:类似于原来django中的分页
				-page_size = api_settings.PAGE_SIZE
				-page_query_param = 'page'
				-page_size_query_param
				-max_page_size
				
				
			第二种:偏移分页
				#每页显示的条数
			    default_limit = api_settings.PAGE_SIZE
				#标杆值
				offset_query_param = 'offset'
				#往后偏移多少
				limit_query_param = 'limit'
				#每页显示最大的条数				
				max_limit = None
				
				****要取的条数,如果超过了,还剩多少,就显示多少
				
			第三种:加密分页(查询速度快)
				#按nid排序
				page.ordering = 'nid'
				#查询的key值
				cursor_query_param = 'cursor' 
				#每页显示多少条
				page_size = api_settings.PAGE_SIZE
				
		使用:
			-生成一个对象(普通,偏移,加密)
				--***重点:可以修改对象的属性
			-ret=对象.paginate_queryset(book_list, request, self)
				
			-丢到序列化类中,完成序列化,
			-最后返回
			
	
	版本控制
		掌握的:URLPathVersioning
		-在setting中配置:
			    'VERSION_PARAM':'version',
				'DEFAULT_VERSION':'v1',
				'ALLOWED_VERSIONS': ['v1', 'v2'],
		-局部使用:
			versioning_class = URLPathVersioning
			
		-全局使用:
			'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning',
		-路由配置:
			url(r'^(?P<version>[v1|v2|v3]+)/books/$', views.Book.as_view({'get':'get_all'}),name='ttt'),
		-在视图类中的request对象中,就可以取出版本号
		-反向解析:
			url2=request.versioning_scheme.reverse('ttt',request=request)

代码示范
from django.shortcuts import render

# Create your views here.
from app01 import models
from rest_framework.viewsets import ViewSetMixin
from rest_framework.views import APIView
from app01 import mySer
from django.http import JsonResponse
from rest_framework.response import Response
# 以后再用settings推荐用它
from django.conf import settings
# class Book(ViewSetMixin, APIView):
#     def get_all(self, request):
#
#         # request.session['my']='ddd'
#         # '''
#         # 生成一个随机字符串:dddsdafadf
#         # dddsdafadf:{my:ddd}
#         # 向cookie中写dddsdafadf
#         # '''
#         # print(settings.AAA)
#
#         # 过滤条件,从GET中取出来
#         response = {'status': 100, 'msg': '查询成功'}
#         book_list = models.Book.objects.all()
#         ser = mySer.BookSerializer(instance=book_list, many=True)
#         response['data'] = ser.data
#         return JsonResponse(response,safe=False)
#         # return Response(response)
#
#     def get_one(self, request,pk):
#         response = {'status': 100, 'msg': '查询成功'}
#         book = models.Book.objects.all().filter(pk=pk).first()
#         if book:
#             ser = mySer.BookSerializer(instance=book, many=False)
#             response['data'] = ser.data
#         else:
#             response['status']=101
#             response['msg']='您要查询的不存在'
#         return Response(response)

# from rest_framework.viewsets import ModelViewSet
#
# from rest_framework.views import  APIView
# class Book(ModelViewSet):
#     queryset = models.Book.objects.all()
#     serializer_class = mySer.BookSerializer

# 普通分页
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


# 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)
# 偏移分页
# 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)

# 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)


from rest_framework.pagination import CursorPagination
from rest_framework.versioning import URLPathVersioning, QueryParameterVersioning, AcceptHeaderVersioning


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)
 
posted @ 2018-12-18 17:03  不沉之月  阅读(41)  评论(0编辑  收藏  举报