DRF最高封装的子类视图
# 转载请留言联系
-
子类视图:
视图 | 作用 | 方法 | 父类 |
---|---|---|---|
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回去而已......
代码成就万世基积沙镇海,梦想永在凌云意意气风发。