Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to use a ViewSet and the APIView mixins on the same view in Django REST Framework?

I'm learning Django REST Framework (http://tomchristie.github.io/rest-framework-2-docs/, we're still on version 2.4). Is it correct to define a class like this, for instance:

class UserView(generics.RetrieveUpdateDestroyAPIView,
               generics.ListCreateAPIView, 
               viewsets.GenericViewSet):
    # ... rest of class

In other words, is it correct or possible to use *ViewSet and *APIView mixins/classes together, or are they intended as two wholly separate concepts, not to be combined?

like image 690
Scott Stafford Avatar asked Jun 02 '15 15:06

Scott Stafford


2 Answers

I don't think it is advisable to use both together. They were built for different purposes.

As per the docs,

ListCreateAPIView:

Used for read-write endpoints to represent a collection of model instances.

Provides get and post method handlers.

Extends: GenericAPIView, ListModelMixin, CreateModelMixin

RetrieveUpdateDestroyAPIView:

Provides get, put, patch and delete method handlers.

Extends: GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin

GenericViewSet:

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.

ModelViewSet:

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

With ModelViewSet you can achieve all the CRUD operations that you intend to use with your above code snippet.

If you want a custom functionality i.e not all the method handlers, you can use generic views. But, if you want all the method handlers, then viewsets comes to your rescue. You can even browse through the rest framework code and see that mixins inherited in generic API views are a subset of mixins inherited in Viewsets. Viewsets basically bundles those views together.

You can just do the following and achieve what you were intending to do in the first place:

class UserView(viewsets.ModelViewSet):
    .....
like image 159
Rahul Gupta Avatar answered Nov 16 '22 19:11

Rahul Gupta


In all versions of Django REST framework, the generic API views and ViewSet classes are distinctly separate, but the mixins can be shared across them. This is because the viewsets actually inherit from the generic classes in the first place.

As stated before, you can use the generic mixins though if you want to build a viewset with only a few supported methods, or if you want to override one of them on your own.

class UserView(mixins.CreateModelMixin, mixins.ListModelMixin,
               mixins.RetrieveModelMixin, mixins.DestroyModelMixin,
               mixins.UpdateModelMixin, 
               viewsets.GenericViewSet):

Don't forget that Django REST framework does provide a ModelViewSet and ReadOnlyModelViewSet base class that you can use as well.

like image 40
Kevin Brown-Silva Avatar answered Nov 16 '22 17:11

Kevin Brown-Silva