<liclass="main"><ahref="#tutorial-3-using-class-based-views">Tutorial 3: Using 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="#reusing-generic-class-based-views">Reusing generic class based views</a></li>
</ul>
</div>
</div>
<divclass="span9">
<h1id="tutorial-3-using-class-based-views">Tutorial 3: Using 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.</p>
<p>So far, so good. It looks pretty similar to the previous case, but we've got better seperation between the different HTTP methods. We'll also need to update the instance view. </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 is going to be pretty simliar for any model-backed API views we create. Those bits of common behaviour are implemented in REST framework's mixin classes.</p>
<p>We can compose those mixin classes, to recreate our existing API behaviour with less code.</p>
<pre><code>from blog.models import Comment
from blog.serializers import CommentSerializer
from djangorestframework import mixins, views
class CommentRoot(mixins.ListModelQuerysetMixin,
mixins.CreateModelInstanceMixin,
views.BaseRootAPIView):
model = Comment
serializer_class = CommentSerializer
get = list
post = create
class CommentInstance(mixins.RetrieveModelInstanceMixin,
mixins.UpdateModelInstanceMixin,
mixins.DestroyModelInstanceMixin,
views.BaseInstanceAPIView):
model = Comment
serializer_class = CommentSerializer
get = retrieve
put = update
delete = destroy
</code></pre>
<h2id="reusing-generic-class-based-views">Reusing generic class based views</h2>
<p>That's a lot less code than before, but we can go one step further still. REST framework also provides a set of already mixed-in views.</p>
<pre><code>from blog.models import Comment
from blog.serializers import CommentSerializer
from djangorestframework import views
class CommentRoot(views.RootAPIView):
model = Comment
serializer_class = CommentSerializer
class CommentInstance(views.InstanceAPIView):
model = Comment
serializer_class = CommentSerializer
</code></pre>
<p>Wow, that's pretty concise. We've got a huge amount for free, and our code looks like
good, clean, idomatic Django.</p>
<p>Next we'll move onto <ahref="4-authentication-permissions-and-throttling.html">part 4 of the tutorial</a>, where we'll take a look at how we can customize the behavior of our views to support a range of authentication, permissions, throttling and other aspects.</p>