Django Rest Framework Tutorial Part 4 Mixins

Requirements

In order to complete this tutorial you need to have completed the previous tutorial: Django Rest Framework Serialization Tutorial Part 3.

Objective

After completing this tutorial you will get a better understanding of the generic and mixin classes available in the Django Rest Framework. I highly recommend that you go through their documentation here: http://www.django-rest-framework.org/api-guide/generic-views/#genericapiview.

The generic views that Django provides and that will be used in this tutorial enables developers to re-use common design patterns. This enables us to develop API views rapidly.

Steps

Update the views.py by importing these packages:

from rest_framework import mixins
from rest_framework import generics

Next we will update the DataRowList class to be the following:

class DataRowList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
queryset = DataRow.objects.all()
serializer_class = DataRowSerializers

def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)

def post(self,request,*args, **kwargs):
return self.create(request, *args, **kwargs)

You will notice that the DataRowList inherits from Multiple classes. Note that the order of the inheritance list in Python matters. The base class is the GenericAPIView. This class indirectly inherits django.views.generic.View which provides the core functionality (.get() and .post() methods amongst other http methods). The child classes are the mixin classes. These classes provides the actions (.list() and .create() methods). The DataRowList simply binds the core functions to the actions.

Next update the DataRowDetail class to be like below:

class DataRowDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView):
queryset = DataRow.objects.all()
serializer_class = DataRowSerializers

def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)

def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)

The design pattern of this class is akin to the class before. We rely on the GenericAPIView class to provide the core functionality and we simply bind these to the actions (.retrieve(), .update() and .destroy()) from the mixin classes.

Go ahead and try retrieving the json data via curl as discussed in Django Rest Framework Serialization Tutorial Part 2.

Wait There is More

So far the design pattern has been inheriting from the GenericAPIView to get the core functionality and binding these to actions inherited from the Mixin classes. Django Rest Framework also provides a set of already Mixed In Generic views that can be inherited to further simplify the views.

Re-write the two classes (DataRowList and DataRowDetail) to match the code below:

class DataRowList(generics.ListCreateAPIView):
queryset = DataRow.objects.all()
serializer_class = DataRowSerializers

class DataRowDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = DataRow.objects.all()
serializer_class = DataRowSerializers

Go ahead and check that the API endpoints still work. 

Conclusion

Now you have gone through the tutorial you can see the benefits of using class based views for API views. Throughout the various tutorials we have simplified the CRUD methods and kept our code base short and simple (DRY).

Subscribe to our mailing list

* indicates required