Using mixins

One of the big wins of using class-based views is that it allows us to easily compose reusable bits of behaviour.

The create/retrieve/update/delete operations that we've been using so far are going to be pretty similar for any model-backed API views we create. Those bits of common behaviour are implemented in REST framework's mixin classes.

Let's take a look at how we can compose the views by using the mixin classes. Here's our views.py module again.

  1. from snippets.models import Snippet
  2. from snippets.serializers import SnippetSerializer
  3. from rest_framework import mixins
  4. from rest_framework import generics
  5. class SnippetList(mixins.ListModelMixin,
  6. mixins.CreateModelMixin,
  7. generics.GenericAPIView):
  8. queryset = Snippet.objects.all()
  9. serializer_class = SnippetSerializer
  10. def get(self, request, *args, **kwargs):
  11. return self.list(request, *args, **kwargs)
  12. def post(self, request, *args, **kwargs):
  13. return self.create(request, *args, **kwargs)

We'll take a moment to examine exactly what's happening here. We're building our view using GenericAPIView, and adding in ListModelMixin and CreateModelMixin.

The base class provides the core functionality, and the mixin classes provide the .list() and .create() actions. We're then explicitly binding the get and post methods to the appropriate actions. Simple enough stuff so far.

  1. class SnippetDetail(mixins.RetrieveModelMixin,
  2. mixins.UpdateModelMixin,
  3. mixins.DestroyModelMixin,
  4. generics.GenericAPIView):
  5. queryset = Snippet.objects.all()
  6. serializer_class = SnippetSerializer
  7. def get(self, request, *args, **kwargs):
  8. return self.retrieve(request, *args, **kwargs)
  9. def put(self, request, *args, **kwargs):
  10. return self.update(request, *args, **kwargs)
  11. def delete(self, request, *args, **kwargs):
  12. return self.destroy(request, *args, **kwargs)

Pretty similar. Again we're using the GenericAPIView class to provide the core functionality, and adding in mixins to provide the .retrieve(), .update() and .destroy() actions.