DRF的视图
APIView
我们django中写CBV的时候继承的是View,rest_framework继承的是APIView,那么他们两个有什么不同呢~~~
urlpatterns = [ url(r '^book$' , BookView.as_view()), url(r '^book/(?P<id>d+)$' , BookEditView.as_view()), ] |
我们可以看到,不管是View还是APIView最开始调用的都是as_view()方法~~那我们走进源码看看~~
我们能看到,APIView继承了View, 并且执行了View中的as_view()方法,最后把view返回了,用csrf_exempt()方法包裹后去掉了csrf的认证。
那我们看看View中的as_view()方法做了什么~
我们看到了~在View中的as_view方法返回了view函数,而view函数执行了self.dispatch()方法~~但是这里的dispatch方法应该是我们APIView中的~~
我们去initialize_request中看下把什么赋值给了request,并且赋值给了self.request, 也就是我们在视图中用的request.xxx到底是什么~~
我们看到,这个方法返回的是Request这个类的实例对象~~我们注意我们看下这个Request类中的第一个参数request,是我们走我们django的时候的原来的request~
我们看到了,这个Request类把原来的request赋值给了self._request, 也就是说以后_request是我们老的request,新的request是我们这个Request类~~
那我们继承APIView之后请求来的数据都在哪呢~~
我们用了rest_framework框架以后,我们的request是重新封装的Request类~
request.query_params 存放的是我们get请求的参数
request.data 存放的是我们所有的数据,包括post请求的以及put,patch请求~~~
相比原来的django的request,我们现在的request更加精简,清晰了~~~
现在我们知道了APIView和View的一些区别~~当然还有~~后面我们还会说~~
我们写的视图可能对多个表进行增删改查,就导致我们的视图特别多重复的代码~~
那么我们尝试着来进行封装一下~~
第一次封装
class BookView(APIView): def get(self, request): book_queryset = models.Book.objects.all() # 用序列化器进行序列化 ser_obj = BookSerializers(book_queryset, many=True) # many=True可以放多个 return Response(ser_obj.data) def post(self, request): book_obj = request.data ser_obj = BookSerializers(data=book_obj) if ser_obj.is_valid(): ser_obj.save() return Response(ser_obj.validated_data) return Response(ser_obj.errors) class BookEditView(APIView): def get(self, request, id): # book_obj = Book.objects.filter(id=id).first() # ser_obj = BookSerializer(book_obj) # return Response(ser_obj.data) def put(self, request, id): # book_obj = Book.objects.filter(id=id).first() # ser_obj = BookSerializer(instance=book_obj, data=request.data, partial=True) # if ser_obj.is_valid(): # ser_obj.save() # return Response(ser_obj.validated_data) # return Response(ser_obj.errors) def delete(self, request, id): # book_obj = Book.objects.filter(id=id).first() # if not book_obj: # return Response("删除的对象不存在") # book_obj.delete() # return Response("")
class GenericAPIView(APIView): queryset = None serializer_class = None def get_queryset(self): return self.queryset.all() def get_serializer(self, *args, **kwargs): return self.serializer_class(*args, **kwargs) class ListModelMixin(object): def list(self, request, *args, **kwargs): queryset = self.get_queryset() serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) class CreateModelMixin(object): def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.validated_data) else: return Response(serializer.errors) class RetrieveModelMixin(object): def retrieve(self, request, id, *args, **kwargs): book_obj = self.get_queryset().filter(pk=id).first() book_ser = self.get_serializer(book_obj) return Response(book_ser.data) class UpdateModelMixin(object): def update(self, request, id, *args, **kwargs): book_obj = self.get_queryset().filter(pk=id).first() book_ser = self.get_serializer(book_obj, data=request.data, partial=True) if book_ser.is_valid(): book_ser.save() return Response(book_ser.validated_data) else: return Response(book_ser.errors) class DestroyModelMixin(object): def destroy(self, request, id, *args, **kwargs): queryset = self.get_queryset() try: queryset.get(pk=id).delete() return Response("") except Exception as e: return Response("信息有误") # 我们把公共的部分抽出来 这样不管写多少表的增删改查都变的很简单 # 这样封装后我们的视图会变成这样 class BookView(GenericAPIView, ListModelMixin, CreateModelMixin): queryset = Book.objects.all() serializer_class = BookSerializer 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 BookEditView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin): queryset = Book.objects.all() serializer_class = BookSerializer def get(self, request, id): return self.retrieve(request, id) def put(self, request, id): return self.update(request, id) def delete(self, request, id): return self.destroy(request, id) 第一次封装
我们封装的GenericAPIView,包括封装每个方法的类,其实框架都帮我们封装好了~~
我们可以直接继承这些类~~来实现上面的视图~~可是还有没有更简单的方法呢~我们再次封装一下~~
第二次封装
from django.shortcuts import render from app01 import models from rest_framework.views import APIView from .serializers import BookSerializers from rest_framework.response import Response from rest_framework.viewsets import ViewSetMixin from rest_framework import viewsets class CreateModelMIxin(object): # 单独抽离post新增方法 def create(self, request): ser_obj = self.get_serializer(data=request.data) if ser_obj.is_valid(): ser_obj.save() return Response(ser_obj.validated_data) return Response(ser_obj.errors) class ListModelMixin(object): # 单独抽离查询所有的get 方法 def list(self, request): quertset = self.get_queryset() ser_obj = self.get_serializer(quertset, many=True) return Response(ser_obj.data) class GenericAPIView(APIView): queryset = None serializer_class = None def get_queryset(self): return self.queryset.all() def get_serializer(self, *args, **kwargs): return self.serializer_class(*args, **kwargs) class ListCreateAPIView(GenericAPIView, CreateModelMIxin, ListModelMixin): pass class BookView(ListCreateAPIView): queryset = models.Book.objects.all() serializer_class = BookSerializers def get(self, request): return self.list(request) def post(self, request): return self.create(request) class RetrieveModelMixin(object): # 单独抽离查询单条的get方法 def retrieve(self, request, id): book_obj = self.get_queryset().filter(id=id).first() ser_obj = self.get_serializer(book_obj) return Response(ser_obj.data) class UpdateModelMixin(object): # 单独抽离put方法 def update(self, request, id): book_obj = self.get_queryset().filter(id=id).first() ser_obj = self.get_serializer(instance=book_obj, data=request.data, partial=True) if ser_obj.is_valid(): ser_obj.save() return Response(ser_obj.validated_data) return Response(ser_obj.errors) class DestroyModelMixin(object): # 单独抽离delete方法 def destroy(self, request, id): book_obj = self.get_queryset().filter(id=id).first() if not book_obj: return Response('删除对象不存在') book_obj.delete() return Response("") class RetrieveUpdateDestroyAPIView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin): pass class BookEditView(RetrieveUpdateDestroyAPIView): queryset = models.Book.objects.all() serializer_class = BookSerializers def get(self, request, id): return self.retrieve(request, id) def put(self, request, id): return self.update(request, id) def delete(self, request, id): return self.destroy(request, id)
这次我们只是让继承变的简单了一点而已,好像并没有什么大的进步~~
我们可不可以把这两个视图合并成一个视图呢~~~框架给我们提供了一个路由传参的方法~~
我们看下ViewSetMixin
actions这个默认参数其实就是我们路由可以进行传参了~~~
下面这个循环~可以看出~我们要传的参数是一个字段~key应该是我们的请求方式,value应该对应我们处理的方法~
这样我们每个视图就不用在写函数了~因为已经和内部实现的函数相对应了~
第三次封装
urlpatterns = [ # path('book_list/', views.BookView.as_view()), # re_path(r'book_list/(?P<id>d+)', views.BookEditView.as_view()), re_path(r'^book_list/', views.BookModelView.as_view({"get": "list", "post": "create"})), re_path(r'^book_list/(?P<pk>d+)', views.BookModelView.as_view({"get": "retrieve", "put": "update", "delete": "destroy"})), ]
from rest_framework.viewsets import ViewSetMixin # class BookView(ViewSetMixin, ListCreateAPIView, RetrieveUpdateDestroyAPIView): # queryset = Book.objects.all() # serializer_class = BookSerializer # 如果我们再定义一个类 class ModelViewSet(ViewSetMixin, ListCreateAPIView, RetrieveUpdateDestroyAPIView): pass class BookView(ModelViewSet): queryset = Book.objects.all() serializer_class = BookSerializer 第三次封装
我们现在的视图就只要写两行就可以了~~~
其实我们写的所有的视图~框架都帮我们封装好了~
注意一点~~用框架封装的视图~我们url上的那个关键字参数要用pk~~系统默认的~~
奉献一张图来看下我们的继承顺序~~~
DRF的路由
我们上面的路由传参写的特别多~~框架也帮我们封装好了~
from django.urls import path, include, re_path from SerDemo import views # 帮我们生成带参数的路由 from rest_framework.routers import DefaultRouter # 实例化DefaultRouter() router = DefaultRouter() # 注册我们的路由以及视图 # router.register('book_list', views.BookModelView) urlpatterns += router.urls
我们可以看到~~通过框架我们可以把路由视图都变的非常简单~~
但是需要自定制的时候还是需要我们自己用APIView写~~当不需要那么多路由的时候~也不要用这种路由注册~~
总之~~一切按照业务需要去用~~~