简体   繁体   English

混淆何时在 Django 中使用 viewsets.Viewset 和 viewsets.ModelViewSet

[英]confusion when to use viewsets.Viewset and viewsets.ModelViewSet in django

According to django rest frame work 3.7 (viewsets.ViewSet) will provide routes for a standard set of create/retrieve/update/destroy style actions根据 django rest frame work 3.7 (viewsets.ViewSet)将为一组标准的创建/检索/更新/销毁样式操作提供路由

and

(viewsets.ModelViewSet) also will provide routes for a standard set of create/retrieve/update/destroy style actions (viewsets.ModelViewSet)还将为一组标准的创建/检索/更新/销毁样式操作提供路由

so when to use this two class and what is the difference between this two.那么什么时候使用这两个类,这两个类有什么区别。 and get_objects() method can we override in (viewsets.ViewSet) class?我们可以在(viewsets.ViewSet)类中覆盖 get_objects() 方法吗? or get_objects() method only limited to (viewsets.ModelViewSet) class?.Thanks或 get_objects() 方法仅限于(viewsets.ModelViewSet)类?。 (viewsets.ModelViewSet)

Maybe somebody else will give a more complete answer, but here's the quick and dirty.也许其他人会给出更完整的答案,但这是快速而肮脏的。 A ModelViewset is a Viewset that is very easy to configure for CRUD operations on your data model. ModelViewset 是一个视图集,它很容易为数据模型上的 CRUD 操作配置。 If you are looking to expose a REST API for an object defined in your models.py, the quickest way to expose that is with a ModelViewSet.如果您希望为在 models.py 中定义的对象公开 REST API,则公开该对象的最快方法是使用 ModelViewSet。 A viewset is much more wide open with respect to application.视图集在应用程序方面更加开放。 You could build a model CRUD endpoint with a Viewset, but you could also build an endpoint that doesn't tie into the model at all.您可以使用 Viewset 构建模型 CRUD 端点,但您也可以构建一个完全不绑定到模型的端点。 You have a lot of flexibility with a ViewSet, but a ModelViewset is more constrained, but requires less configuration to accomplish most model based tasks. ViewSet 有很大的灵活性,但 ModelViewset 受更多约束,但需要较少的配置来完成大多数基于模型的任务。

i need to add some more details.我需要添加更多细节。 i am using doc code explain further我正在使用文档代码进一步解释

  1. viewsets.ViewSet视图集.ViewSet

     class ViewSet(ViewSetMixin, views.APIView): """ The base ViewSet class does not provide any actions by default. """ pass

this means ViewSet inherited two class ViewSetMixin (it gives just binding the 'GET' and 'POST' methods to the 'list' and 'create' actions) and views.APIView (this gives authentication_classes, permission_classes, etc... attributes ).这意味着ViewSet继承了两个类ViewSetMixin (它只将 'GET' 和 'POST' 方法绑定到 'list' 和 'create' 操作)和views.APIView (这提供了 authentication_classes、permission_classes 等...属性)。 so viewsets.ViewSet does not provide any concrete actions methods by default but you have to manualy override list,create,update, etc... methods.所以默认情况下, viewsets.ViewSet不提供任何具体的操作方法,但您必须手动覆盖列表、创建、更新等...方法。

  1. viewsets.ModelViewSet视图集.ModelViewSet
class ModelViewSet(mixins.CreateModelMixin,
                        mixins.RetrieveModelMixin,
                        mixins.UpdateModelMixin,
                        mixins.DestroyModelMixin,
                        mixins.ListModelMixin,
                        GenericViewSet):
         """
         A viewset that provides default create(),retrieve(),update(),
         partial_update(), destroy() and list() actions.
         """
         pass

this means ModelViewSet inherited all most all mixins so it provides default list,create,update etc.. action methods and GenericViewSet ( it provides the get_object and get_queryset methods, You'll need to either set these attributes, or override get_queryset() / get_serializer_class() because GenericViewSet inhereted from GenericAPIView,so modelViewSet requires queryset and serializer_class attributes set in ModelViewSet.这意味着ModelViewSet继承了几乎所有的 mixin,因此它提供了默认列表、创建、更新等。操作方法和GenericViewSet (它提供了get_objectget_queryset方法,您需要设置这些属性,或者覆盖get_queryset() / get_serializer_class()因为GenericViewSet继承自GenericAPIView,所以modelViewSet需要在ModelViewSet中设置querysetserializer_class属性。

3. get_objects() method can we override in (viewsets.ViewSet) class? 3. get_objects() 方法可以在(viewsets.ViewSet) 类中重写吗? or get_objects() method only limited to (viewsets.ModelViewSet) class?.还是 get_objects() 方法只限于 (viewsets.ModelViewSet) 类?。

**get_object** and **get_queryset** belongs to **GenericViewSet(GenericAPIView)** class, in ModelViewSet this GenericViewSet inherited by default so it works only in **ModelViewSet** and **get_object** method no use in ViewSet.

for more info check this article , next time you wont ask an question欲了解更多信息,请查看这篇文章,下次你不会再问问题了

I agree with the above answer and just wanted to add some more points to it.我同意上面的答案,只是想补充一些要点。 The ModelViewset is similar to standard Viewset but it is made to manage model and have several built in functionality.Also we can override the default object manager functions like create, update ,etc as per our need.So yes ModelViewset is made to manage database objects for a model. ModelViewset 与标准 Viewset 类似,但它用于管理模型并具有多个内置功能。此外,我们可以根据需要覆盖默认的对象管理器功能,如创建、更新等。所以是的 ModelViewset 用于管理数据库对象对于一个模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM