<ulclass="nav nav-list side-nav well sidebar-nav-fixed">
<liclass="main">
<ahref="#tutorial-3-class-based-views">Tutorial 3: Class Based Views</a>
</li>
<li>
<ahref="#rewriting-our-api-using-class-based-views">Rewriting our API using class based views</a>
</li>
<li>
<ahref="#using-mixins">Using mixins</a>
</li>
<li>
<ahref="#using-generic-class-based-views">Using generic class based views</a>
</li>
</ul>
</div>
</div>
<divid="main-content"class="span9">
<h1id="tutorial-3-class-based-views">Tutorial 3: Class Based Views</h1>
<p>We can also write our API views using class based views, rather than function based views. As we'll see this is a powerful pattern that allows us to reuse common functionality, and helps us keep our code <ahref="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a>.</p>
<h2id="rewriting-our-api-using-class-based-views">Rewriting our API using class based views</h2>
<p>We'll start by rewriting the root view as a class based view. All this involves is a little bit of refactoring of <code>views.py</code>.</p>
<pre><code>from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
<p>So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in <code>views.py</code>.</p>
<p>Okay, we're done. If you run the development server everything should be working just as before.</p>
<h2id="using-mixins">Using mixins</h2>
<p>One of the big wins of using class based views is that it allows us to easily compose reusable bits of behaviour.</p>
<p>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.</p>
<p>Let's take a look at how we can compose the views by using the mixin classes. Here's our <code>views.py</code> module again.</p>
<pre><code>from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import mixins
from rest_framework import generics
class SnippetList(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
</code></pre>
<p>We'll take a moment to examine exactly what's happening here. We're building our view using <code>GenericAPIView</code>, and adding in <code>ListModelMixin</code> and <code>CreateModelMixin</code>.</p>
<p>The base class provides the core functionality, and the mixin classes provide the <code>.list()</code> and <code>.create()</code> actions. We're then explicitly binding the <code>get</code> and <code>post</code> methods to the appropriate actions. Simple enough stuff so far.</p>
<p>Pretty similar. Again we're using the <code>GenericAPIView</code> class to provide the core functionality, and adding in mixins to provide the <code>.retrieve()</code>, <code>.update()</code> and <code>.destroy()</code> actions.</p>
<h2id="using-generic-class-based-views">Using generic class based views</h2>
<p>Using the mixin classes we've rewritten the views to use slightly less code than before, but we can go one step further. REST framework provides a set of already mixed-in generic views that we can use to trim down our <code>views.py</code> module even more.</p>
<pre><code>from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import generics
class SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
</code></pre>
<p>Wow, that's pretty concise. We've gotten a huge amount for free, and our code looks like good, clean, idiomatic Django.</p>
<p>Next we'll move onto <ahref="../4-authentication-and-permissions">part 4 of the tutorial</a>, where we'll take a look at how we can deal with authentication and permissions for our API.</p>