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
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.
i need to add some more details. i am using doc code explain further
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With