DRF視圖python
APIviewdjango
django中寫CBV的時候是繼承View,而rest_framework繼承的是APIView,那麼他們兩個有什麼不一樣ide
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的認證。post
而後看view中as_view()方法作了什麼url
在View中的as_view方法返回了view函數,而view函數執行了self.dispatch()方法 ,可是這裏的dispatch方法按照順序應該從本身開始尋找,而在APIview中,也寫了dispatch方法,因此應該執行的是APIview中的dispatch方法spa
這裏我我須要注意initialize_request中到底作了什麼3d
這裏返回的是一個實例化對象rest
這會django原先的request變成了self._requestcode
視圖
本身寫的版本
# 本身重寫的視圖 from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.viewsets import ViewSetMixin # 獲取要的數據 class GenericAPIView(APIView): queryset = None serializer_class = None # 獲取queryset def get_queryset(self): return self.queryset.all() # 獲取序列化器 def get_serializer(self, *args, **kwargs): return self.serializer_class(*args, **kwargs) # get方法(查詢多條) class ListModelMixin(object): def list(self, request): queryset = self.get_queryset() ser_obj = self.get_serializer(queryset, many=True) return Response(ser_obj.data) # 查詢單條 class RetrieveModelMixin(object): def retrieve(self, request, id): stu_obj = self.get_queryset().filter(id=id).first() ser_obj = self.get_serializer(stu_obj) return Response(ser_obj.data) # post方法 class CreateModelMixin(object): def create(self, request): ser_obj = self.get_serializer(data=request.data) if ser_obj.is_valid(): ser_obj.save() return Response(ser_obj.data) else: return Response(ser_obj.errors) # put/patch方法:修改單個的 class UpdateModelMixin(object): def update(self, request, id): stu_obj = self.get_queryset().filter(id=id).first() ser_obj = self.get_serializer(instance=stu_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) # delete方法 class DestroyModelsMixin(object): def destroy(self, request, id): stu_obj = self.get_queryset().filter(id=id).first() if stu_obj: stu_obj.delete() return Response("") return Response("刪除的對象不存在") # # 全部方法只要繼承這個就能夠 class ModelViewSet( ViewSetMixin, GenericAPIView, ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelsMixin, ): pass class Student(ModelViewSet): queryset = models.Student.objects serializer_class = StudentSerializer
他還有一個自帶的模塊和這個同樣
from rest_framework.viewsets import ModelViewSet class Student(ModelViewSet): queryset = models.Student.objects serializer_class = StudentSerializer
urls.py代碼
DRF的路由
這個路由其實也封裝好了,可是封裝越好的東西,應用場景限制的也越多
from .views import BookView from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register(r"book", BookView) urlpatterns = [ # url(r'^book$', BookView.as_view()), # url(r'^book/(?P<id>\d+)$', BookEditView.as_view()), # url(r'^book$', BookView.as_view({"get": "list", "post": "create"})), # url(r'^book/(?P<pk>\d+)$', BookView.as_view({"get": "retrieve", "patch": "update", "delete": "destroy"})), ] urlpatterns += router.urls