DRF的视图类、究竟改如何选择继承???

1、视图类

1.1 --最基本的两个视图类、
from rest_framework.views import APIView
1、APIView--扩展性最强、方法需要自己写、

from rest_framework.generics import GenericAPIView

2、GenericAPIView(APIView):继承APIView对其进行一些封装 
	-queryset = models.Publish.objects.all()  # 要序列化的数据
    -serializer_class = serializer.PublishSerializer
    每一个方法使用--get_object()和get_serializer()序列化--五个接口写五个
    --有关数据库的操作使用需要继承
    -属性
    	queryset
        serializer_class
    -方法
    	get_queryset(self):获取qs数据
    	get_object(self): 获取一条数据对象
        get_serializer(self, *args, **kwargs): 实例化得到ser对象
		get_serializer_class(self): 获取序列化类
1.2 --5个视图扩展类–单接口继承
from rest_framework.mixins import....

    1、查所有--ListModelMixin
    2、增一个--CreateModelMixin
    3、查一个--RetrieveModelMixin
    4、改一个--UpdateModelMixin
    5、删一个--DestroyModelMixin
1.3 --9个视图子类
GenericAPIView+5个视图扩展类
CreateAPIView:-----有post方法,新增数据
	#继承CreateModelMixin,GenericAPIView,
DestroyAPIView:---有delete方法,删除数据
	#继承DestroyModelMixin,GenericAPIView,
ListAPIView:--------有get方法获取所有
	#继承ListModelMixin,GenericAPIView,
UpdateAPIView:------有put和patch方法,修改数据
	#继承UpdateModelMixin,GenericAPIView,
RetrieveAPIView:-----有get方法,获取一条
	#继承RetrieveModelMixin,GenericAPIView,

ListCreateAPIView:---有get获取所有,post方法新增
	#继承ListModelMixin,CreateModelMixin,GenericAPIView,
RetrieveDestroyAPIView: --有get方法获取一条,delete方法删除
	#继承RetrieveModelMixin,DestroyModelMixin,GenericAPIView,
RetrieveUpdateAPIView:---有get获取一条,put,patch修改
	#继承RetrieveModelMixin,UpdateModelMixin,GenericAPIView,
RetrieveUpdateDestroyAPIView:--有get获取一条,put,patch修改,delete删除
	#继承RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView,

#视图集
ViewSetMixin:重写了as_view 
ViewSet:     继承ViewSetMixin和APIView

GenericViewSet:继承ViewSetMixin, generics.GenericAPIView
ModelViewSet:继承mixins.CreateModelMixin,mixins.RetrieveModelMixin,mixins.UpdateModelMixin,mixins.DestroyModelMixin,mixins.ListModelMixin,GenericViewSet
ReadOnlyModelViewSet:继承mixins.RetrieveModelMixin,mixins.ListModelMixin,GenericViewSet

GenericAPIView+5个视图扩展类
	在GenericAPIView的基础上 对get_object()和get_serializer()封装到一个类中
    继承之后就不用重复写
    但是还是需要两个类、对应两个不同的get
1.4 最高封装—最简单的用法
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from rest_framework.viewsets import ViewSet, GenericViewSet, ViewSetMixin

ViewSetMixin--对五个接口进行了封装、每个请求反射不同的方法
for method, action in actions.items():
    handler = getattr(self, action)
    setattr(self, method, handler)
so-继承了ViewSetMixin的类、一步到位、
class DepartmentView(ModelViewSet): #ModelViewSet继承了 ViewSetMixin
    queryset = models.Department.objects.all()
    serializer_class = serializer.DepartmentModelSerializer
配合路由层、就可以写出五个接口


###如果视图类继承了ViewSetMixin这个类,路由的as_view执行的是ViewSetMixin的as_view
 path('publish/', views.PublishView.as_view({'get': 'list', 'post': 'create'})),
class ViewSetMixin:        #对路由进行一个反射判断、不同路由过来执行不同方法
    for method, action in actions.items():
        handler = getattr(self, action)
        setattr(self, method, handler)
class PublishView(ModelViewSet):
    queryset = models.Publish.objects.all()  # 要序列化的数据
    serializer_class = serializer.PublishSerializer  # 要序列化的类
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值