在 Django REST Framework 中的同一视图上使用 ViewSet 和 APIView 混合是否正确?
Is it correct to use a ViewSet and the APIView mixins on the same view in Django REST Framework?
我正在学习 Django REST Framework(http://tomchristie.github.io/rest-framework-2-docs/,我们仍在使用 2.4 版)。这样定义 class 是否正确,例如:
class UserView(generics.RetrieveUpdateDestroyAPIView,
generics.ListCreateAPIView,
viewsets.GenericViewSet):
# ... rest of class
换句话说,将 *ViewSet 和 *APIView mixins/classes 一起使用是否正确或可能,或者它们是作为两个完全独立的概念而不是合并使用?
我认为不建议同时使用两者。它们是为不同的目的而建造的。
根据文档,
创建列表API查看:
Used for read-write endpoints to represent a collection of model
instances.
Provides get and post method handlers.
Extends: GenericAPIView, ListModelMixin, CreateModelMixin
检索更新销毁API查看:
Provides get, put, patch and delete method handlers.
Extends: GenericAPIView, RetrieveModelMixin, UpdateModelMixin,
DestroyModelMixin
通用视图集:
The GenericViewSet class inherits from GenericAPIView, and provides
the default set of get_object, get_queryset methods and other generic
view base behavior, but does not include any actions by default.
模型视图集:
The ModelViewSet class inherits from GenericAPIView and includes
implementations for various actions, by mixing in the behavior of the
various mixin classes.
The actions provided by the ModelViewSet class are .list(),
.retrieve(), .create(), .update(), and .destroy().
使用 ModelViewSet
您可以实现您打算与上述代码片段一起使用的所有 CRUD 操作。
如果您想要自定义功能,即不是所有方法处理程序,您可以使用通用视图。但是,如果您需要所有的方法处理程序,那么视图集可以为您提供帮助。您甚至可以浏览其余框架代码并看到在通用 API 视图中继承的 mixins 是在 Viewsets 中继承的 mixins 的子集。
视图集基本上将这些视图捆绑在一起。
您只需执行以下操作并实现您最初打算做的事情:
class UserView(viewsets.ModelViewSet):
.....
在 Django REST 框架的所有版本中,通用 API 视图和 ViewSet classes 是明显分开的,但是 mixin 可以在它们之间共享。这是因为视图集实际上首先继承自通用 classes。
如前所述,如果您只想使用几个受支持的方法构建视图集,或者如果您想自己覆盖其中一个方法,则可以使用通用 mixins。
class UserView(mixins.CreateModelMixin, mixins.ListModelMixin,
mixins.RetrieveModelMixin, mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
不要忘记 Django REST 框架确实提供了 ModelViewSet
和 ReadOnlyModelViewSet
基础 class,您也可以使用。
我正在学习 Django REST Framework(http://tomchristie.github.io/rest-framework-2-docs/,我们仍在使用 2.4 版)。这样定义 class 是否正确,例如:
class UserView(generics.RetrieveUpdateDestroyAPIView,
generics.ListCreateAPIView,
viewsets.GenericViewSet):
# ... rest of class
换句话说,将 *ViewSet 和 *APIView mixins/classes 一起使用是否正确或可能,或者它们是作为两个完全独立的概念而不是合并使用?
我认为不建议同时使用两者。它们是为不同的目的而建造的。
根据文档,
创建列表API查看:
Used for read-write endpoints to represent a collection of model instances.
Provides get and post method handlers.
Extends: GenericAPIView, ListModelMixin, CreateModelMixin
检索更新销毁API查看:
Provides get, put, patch and delete method handlers.
Extends: GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin
通用视图集:
The GenericViewSet class inherits from GenericAPIView, and provides the default set of get_object, get_queryset methods and other generic view base behavior, but does not include any actions by default.
模型视图集:
The ModelViewSet class inherits from GenericAPIView and includes implementations for various actions, by mixing in the behavior of the various mixin classes.
The actions provided by the ModelViewSet class are .list(), .retrieve(), .create(), .update(), and .destroy().
使用 ModelViewSet
您可以实现您打算与上述代码片段一起使用的所有 CRUD 操作。
如果您想要自定义功能,即不是所有方法处理程序,您可以使用通用视图。但是,如果您需要所有的方法处理程序,那么视图集可以为您提供帮助。您甚至可以浏览其余框架代码并看到在通用 API 视图中继承的 mixins 是在 Viewsets 中继承的 mixins 的子集。 视图集基本上将这些视图捆绑在一起。
您只需执行以下操作并实现您最初打算做的事情:
class UserView(viewsets.ModelViewSet):
.....
在 Django REST 框架的所有版本中,通用 API 视图和 ViewSet classes 是明显分开的,但是 mixin 可以在它们之间共享。这是因为视图集实际上首先继承自通用 classes。
如前所述,如果您只想使用几个受支持的方法构建视图集,或者如果您想自己覆盖其中一个方法,则可以使用通用 mixins。
class UserView(mixins.CreateModelMixin, mixins.ListModelMixin,
mixins.RetrieveModelMixin, mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
不要忘记 Django REST 框架确实提供了 ModelViewSet
和 ReadOnlyModelViewSet
基础 class,您也可以使用。