Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

and

(viewsets.ModelViewSet) also will provide routes for a standard set of create/retrieve/update/destroy style actions

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? or get_objects() method only limited to (viewsets.ModelViewSet) class?.Thanks

like image 452
giveJob Avatar asked Apr 12 '18 05:04

giveJob


2 Answers

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. 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. 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. 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.

like image 96
sahutchi Avatar answered Sep 28 '22 12:09

sahutchi


i need to add some more details. i am using doc code explain further

  1. viewsets.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 ). so viewsets.ViewSet does not provide any concrete actions methods by default but you have to manualy override list,create,update, etc... methods.

  1. viewsets.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.

3.get_objects() method can we override in (viewsets.ViewSet) class? or get_objects() method only limited to (viewsets.ModelViewSet) class?.

**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

like image 20
giveJob Avatar answered Sep 28 '22 12:09

giveJob