两个视图基类
1.drf提供了一个顶层的视图APIView,可以通过继承APIView写视图,之后我们写的代码可能重复代码比较多,就可以使用面向对象的继承、封装,GenericAPIView继承了APIView,我们用GenericAPIView时就不需要继承APIView
2.GenericAPIView的属性和方法
2.1属性
-queryset
要序列化的数据
-serializer_class
序列化类
-lookup_field
通过get_object获取单个对象的查询key值,value值是由路由中传进来的
-filter_backends
过滤器
-pagination_class
分页器
2.2方法
-get_queryset
获取要序列化的数据
-get_object
根据lookup_field配置的参数获取一条数据
-get_serializer
获取序列化类,我们直接使用
-get_serializer_class
获取序列化类,不是我们直接使用的,它在get_serializer里调用了它
-filter_queryset
跟过滤有关系
-paginate_xxx
跟分页有关系
5个视图扩展类写接口
'必须配合GenericAPIView使用'
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin
'''
ListModelMixin:写了list方法,就是我们原来的获取所有数据的get中的代码
CreateModelMixin:写了create方法,就是我们原来的post中的代码
RetrieveModelMixin:写了retrieve方法,就是我们原来的获取单条数据的get中的代码
UpdateModelMixin:写了update方法,就是我们原来的put中的代码
DestroyModelMixin:写了destroy方法,就是我们原来的delete中的代码
'''
class BookView(GenericAPIView, ListModelMixin, CreateModelMixin):
queryset = Book.objects.all()
serializer_class = BookModelSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
class BookDataView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
queryset = Book.objects.all()
serializer_class = BookModelSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
9个视图子类写接口
'视图类继承GenericAPIView+某个某几个视图扩展类'
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, RetrieveUpdateAPIView, RetrieveDestroyAPIView
'没有Update+Destroy'
class BookView(ListCreateAPIView):
queryset = Book.objects.all()
serializer_class = BookModelSerializer
class BookDataView(RetrieveUpdateDestroyAPIView):
queryset = Book.objects.all()
serializer_class = BookModelSerializer
视图集
继承ModelViewSet编写5个接口
1.视图类
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
'ReadOnlyModelViewSet只能查询一个或查询所有'
class BookView(ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookModelSerializer
2.路由
urlpatterns = [
path('admin/', admin.site.urls),
path('book/', BookView.as_view({'get': 'list', 'post': 'create'})),
path('book/<str:pk>/', BookView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
]
ViewSetMixin源码
# ViewSetMixin:它控制了路由的写法变了
# ViewSet:APIView+ViewSetMixin的组合
# GenericViewSet:GenericAPIView+ViewSetMixin的组合
'''
ModelViewSet---继承了下面一堆,在路由中配置as_view,执行的就是ViewSetMixin的as_viewViewSetMixin的as_view
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet
'''
-在ViewSetMixin里的as_view中
@classonlymethod
def as_view(cls, actions=None, **initkwargs):
# 路由中as_view中必须传参数,必须传字典:{'get': 'list', 'post': 'create'}
if not actions:
raise TypeError("The `actions` argument must be provided when "
"calling `.as_view()` on a ViewSet. For example "
"`.as_view({'get': 'list'})`")
-路由匹配成功,执行view(request),request是老的request
def view(request, *args, **kwargs):
# actions是字典:{'get': 'list', 'post': 'create'}
for method, action in actions.items():
# method:get action:list
# self是视图类的对象中通过反射,查找list
# handler是视图类中的list方法
handler = getattr(self, action)
# 向视图类的对象中反射method(get),handler:(list)方法
# self.get=list
setattr(self, method, handler)
return self.dispatch(request, *args, **kwargs)
return csrf_exempt(view)
'''
只要继承了ViewSetMixin,以后路由写法变量,都要写成:
BookView.as_view({'get': 'list', 'post': 'create'})
这样写好后,对应请求方式来了,就会执行配置的方法
'''
# 视图类中继承了ViewSetMixin,就可以写任意名字的方法,不用非得写get...
总
1.APIView:它有as_view,dispatch
2.GenericAPIView:继承了APIView,他有类属性和方法
3.5个视图扩展类:ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin
-每个类里有一个方法
4.9个视图扩展类
ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, RetrieveUpdateAPIView, RetrieveDestroyAPIView
标签:get,self,request,视图,class,图集,view
From: https://www.cnblogs.com/riuqi/p/16750156.html