2024-05-23 20:08:23 发布
网友
我刚开始学习Django Rest框架,现在开始学习APIView、ViewSet、ModelViewSet和GenericView的4个概念。它们之间的区别是什么?在RESTAPI的开发中使用哪一个更有效?为什么
没有标准,但您可以从以下简单策略开始:
一个类中的多个动作和基本积垢->Viewset(ModelViewSet和ReadOnlyModelViewSet最有用)
ModelViewSet
ReadOnlyModelViewSet
对模型类的特定操作->Generic Views(RetrieveModelMixin,ListAPIView,UpdateModelMixin,DestroyModelMixin,CreateModelMixin)
RetrieveModelMixin
ListAPIView
UpdateModelMixin
DestroyModelMixin
CreateModelMixin
如果要写入所有create,update。。。方法您自己可以使用APIView并将它们写入get、post、put、。。。方法(APIView和GenericAPIView之间的区别在于,您不能在类中使用某些内置方法,如queryset、serializer_class、…)
create
update
APIView
get
post
put
GenericAPIView
queryset
serializer_class
如果您想从头开始编写所有内容,可以在function base中编写API
function base
I use GenericAPIView in normal APIs & use APIView in the custom ones (like most of APIs in Admin Panel)
没有标准,但您可以从以下简单策略开始:
一个类中的多个动作和基本积垢->Viewset(
ModelViewSet
和ReadOnlyModelViewSet
最有用)对模型类的特定操作->Generic Views(
RetrieveModelMixin
,ListAPIView
,UpdateModelMixin
,DestroyModelMixin
,CreateModelMixin
)如果要写入所有
create
,update
。。。方法您自己可以使用APIView
并将它们写入get
、post
、put
、。。。方法(APIView
和GenericAPIView
之间的区别在于,您不能在类中使用某些内置方法,如queryset
、serializer_class
、…)如果您想从头开始编写所有内容,可以在
function base
中编写API相关问题 更多 >
编程相关推荐