DRF最高封裝的子類檢視
阿新 • • 發佈:2018-11-11
# 轉載請留言聯絡
-
子類檢視:
檢視 | 作用 | 方法 | 父類 |
---|---|---|---|
ListAPIView | 查詢多條資料 | get | GenericAPIView ListModelMixin |
CreateAPIView | 新增一條資料 | post | GenericAPIView CreateModelMixin |
RetrieveAPIView | 查詢一條資料 | get | GenericAPIView RetrieveModelMixin |
UpdateAPIView | 修改一條資料 | put,patch | GenericAPIView UpdateModelMixin |
DestroyAPIView | 刪除一條資料 | delete | GenericAPIView DestroyModelMixin |
RetrieveUpdateAPIView | 查詢,更新一條資料 | get,put,patch | GenericAPIView RetrieveModelMixin UpdateModelMixin |
RetrieveUpdateDestroyAPIView |
獲取,更新,刪除一條資料 | get,put,patch,delete | GenericAPIView RetrieveModelMixin UpdateModelMixin DestroyModelMixin |
-
對比以及使用(GenericAPIView+五個拓展類 VS 子類檢視)
1.GenericAPIView+五個拓展類
class DepartmentListAPIView(ListModelMixin,CreateModelMixin,GenericAPIView): queryset = Department.objects.all() serializer_class= DepartmentSerializer def get(self,request): """查詢多條資料""" return self.list(request) def post(self,request): """新增一條資料""" return self.create(request) class DepartmentDetailAPIView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView): queryset = Department.objects.all() serializer_class = DepartmentSerializer def get(self,request,pk): """查詢一條資料""" return self.retrieve(request,pk) def post(self,request,pk): """修改部門""" return self.update(request,pk) def delete(self,request,pk): """刪除一個部門""" return self.destroy(request,pk)
2.子類檢視
class DepartmentListAPIView(ListAPIView,CreateAPIView): ”“”實現了查詢多條資料和新增一條資料的功能""" queryset = Department.objects.all() serializer_class = DepartmentSerializer class DepartmentDetailAPIView(RetrieveUpdateDestroyAPIView): """實現了查詢一條資料、修改一條資料、刪除一條資料的功能""" queryset = Department.objects.all() serializer_class = DepartmentSerializer
3.子類檢視的原始碼分析
# Concrete view classes that provide method handlers # by composing the mixin classes with the base view. class CreateAPIView(mixins.CreateModelMixin, GenericAPIView): """ Concrete view for creating a model instance. """ def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) class ListAPIView(mixins.ListModelMixin, GenericAPIView): """ Concrete view for listing a queryset. """ def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) class RetrieveAPIView(mixins.RetrieveModelMixin, GenericAPIView): """ Concrete view for retrieving a model instance. """ def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) class DestroyAPIView(mixins.DestroyModelMixin, GenericAPIView): """ Concrete view for deleting a model instance. """ def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs) class UpdateAPIView(mixins.UpdateModelMixin, GenericAPIView): """ Concrete view for updating a model instance. """ def put(self, request, *args, **kwargs): return self.update(request, *args, **kwargs) def patch(self, request, *args, **kwargs): return self.partial_update(request, *args, **kwargs) class ListCreateAPIView(mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView): """ Concrete view for listing a queryset or creating a model instance. """ 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 RetrieveUpdateAPIView(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, GenericAPIView): """ Concrete view for retrieving, updating a model instance. """ 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 patch(self, request, *args, **kwargs): return self.partial_update(request, *args, **kwargs) class RetrieveDestroyAPIView(mixins.RetrieveModelMixin, mixins.DestroyModelMixin, GenericAPIView): """ Concrete view for retrieving or deleting a model instance. """ def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs) class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, GenericAPIView): """ Concrete view for retrieving, updating or deleting a model instance. """ 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 patch(self, request, *args, **kwargs): return self.partial_update(request, *args, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs)
實際上就是把(GenericAPIView+五個拓展類)的函式封裝一下,return回去而已......