标签:pat *args framework 获取 对比 class lazy post color
from rest_framework.mixins import ListModelMixin
我们可以发现,两处的代码基本一致,因此我们直接拿到使用来优化本地代码
a.视图类继承ListModelMixin
b.get方法中直接return self.list(request, *args, **kwargs)
def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs)
from rest_framework.mixins import CreateModelMixin
依然也是基本一致
a.视图类继承CreateModelMixin
b.post方法中直接return self.create(request, *args, **kwargs)
def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs)
from rest_framework.mixins import RetrieveModelMixin
a.视图类继承RetrieveModelMixin
b.get方法中直接return self.retrieve(request, *args, **kwargs)
def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs)
from rest_framework.mixins import UpdateModelMixin
注:这里有一点不一样,源码中有一个‘partial’,这个表示可以进行部分更新,因此update方法不仅支持全更新,也可以支持部分更新
a.视图类继承UpdateModelMixin
b.put方法中直接return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs): return self.update(request, *args, **kwargs)
from rest_framework.mixins import DestroyModelMixin
a.视图类继承DestroyModelMixin
b.delete方法中直接return self.retrieve(request, *args, **kwargs)
def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs)
标签:pat *args framework 获取 对比 class lazy post color
原文地址:https://www.cnblogs.com/xiaogongjin/p/13339535.html