django-rest-framework/docs/tutorial/3-class-based-views.md

154 lines
6.4 KiB
Markdown
Raw Normal View History

2012-09-07 12:37:06 +04:00
# Tutorial 3: Class Based Views
2012-08-29 23:57:37 +04:00
2012-09-19 16:02:10 +04:00
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 [DRY][dry].
2012-08-29 23:57:37 +04:00
## Rewriting our API using class based views
We'll start by rewriting the root view as a class based view. All this involves is a little bit of refactoring.
from blog.models import Comment
from blog.serializers import CommentSerializer
2012-08-29 23:57:37 +04:00
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
2012-08-29 23:57:37 +04:00
2012-09-03 19:42:57 +04:00
class CommentRoot(APIView):
2012-08-29 23:57:37 +04:00
"""
List all comments, or create a new comment.
"""
2012-08-29 23:57:37 +04:00
def get(self, request, format=None):
comments = Comment.objects.all()
serializer = CommentSerializer(instance=comments)
2012-08-29 23:57:37 +04:00
return Response(serializer.data)
def post(self, request, format=None):
serializer = CommentSerializer(request.DATA)
2012-08-29 23:57:37 +04:00
if serializer.is_valid():
comment = serializer.object
comment.save()
return Response(serializer.serialized, status=status.HTTP_201_CREATED)
return Response(serializer.serialized_errors, status=status.HTTP_400_BAD_REQUEST)
2012-09-03 19:42:57 +04:00
2012-08-29 23:57:37 +04:00
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.
2012-09-03 19:42:57 +04:00
class CommentInstance(APIView):
2012-08-29 23:57:37 +04:00
"""
Retrieve, update or delete a comment instance.
"""
2012-08-29 23:57:37 +04:00
def get_object(self, pk):
try:
2012-09-03 19:42:57 +04:00
return Comment.objects.get(pk=pk)
except Comment.DoesNotExist:
2012-08-29 23:57:37 +04:00
raise Http404
2012-08-29 23:57:37 +04:00
def get(self, request, pk, format=None):
comment = self.get_object(pk)
serializer = CommentSerializer(instance=comment)
return Response(serializer.data)
2012-08-29 23:57:37 +04:00
def put(self, request, pk, format=None):
comment = self.get_object(pk)
serializer = CommentSerializer(request.DATA, instance=comment)
if serializer.is_valid():
2012-09-25 15:21:35 +04:00
comment = serializer.object
2012-08-29 23:57:37 +04:00
comment.save()
return Response(serializer.data)
2012-09-17 23:19:45 +04:00
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
2012-08-29 23:57:37 +04:00
def delete(self, request, pk, format=None):
comment = self.get_object(pk)
comment.delete()
2012-09-03 19:42:57 +04:00
return Response(status=status.HTTP_204_NO_CONTENT)
2012-08-29 23:57:37 +04:00
2012-09-03 19:42:57 +04:00
That's looking good. Again, it's still pretty similar to the function based view right now.
2012-09-17 23:19:45 +04:00
We'll also need to refactor our URLconf slightly now we're using class based views.
from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
2012-09-17 23:19:45 +04:00
from blogpost import views
urlpatterns = patterns('',
url(r'^$', views.CommentRoot.as_view()),
url(r'^(?P<pk>[0-9]+)$', views.CommentInstance.as_view())
)
urlpatterns = format_suffix_patterns(urlpatterns)
2012-08-29 23:57:37 +04:00
Okay, we're done. If you run the development server everything should be working just as before.
## Using mixins
One of the big wins of using class based views is that it allows us to easily compose reusable bits of behaviour.
2012-09-07 12:37:06 +04:00
The create/retrieve/update/delete operations that we've been using so far are 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.
2012-08-29 23:57:37 +04:00
2012-09-07 12:37:06 +04:00
Let's take a look at how we can compose our views by using the mixin classes.
2012-08-29 23:57:37 +04:00
from blog.models import Comment
from blog.serializers import CommentSerializer
from rest_framework import mixins
from rest_framework import generics
2012-08-29 23:57:37 +04:00
2012-09-03 20:49:22 +04:00
class CommentRoot(mixins.ListModelMixin,
mixins.CreateModelMixin,
2012-09-07 12:37:06 +04:00
generics.MultipleObjectBaseView):
2012-08-29 23:57:37 +04:00
model = Comment
serializer_class = CommentSerializer
2012-09-03 20:49:22 +04:00
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
2012-08-29 23:57:37 +04:00
2012-09-07 12:37:06 +04:00
We'll take a moment to examine exactly what's happening here - We're building our view using `MultipleObjectBaseView`, 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 explictly binding the `get` and `post` methods to the appropriate actions. Simple enough stuff so far.
2012-09-03 20:49:22 +04:00
class CommentInstance(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
2012-09-07 12:37:06 +04:00
generics.SingleObjectBaseView):
2012-08-29 23:57:37 +04:00
model = Comment
serializer_class = CommentSerializer
2012-09-03 20:49:22 +04:00
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)
2012-08-29 23:57:37 +04:00
2012-09-07 12:37:06 +04:00
Pretty similar. This time we're using the `SingleObjectBaseView` class to provide the core functionality, and adding in mixins to provide the `.retrieve()`, `.update()` and `.destroy()` actions.
## Using generic class based views
2012-08-29 23:57:37 +04:00
2012-09-07 12:37:06 +04:00
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.
2012-08-29 23:57:37 +04:00
from blog.models import Comment
from blog.serializers import CommentSerializer
from rest_framework import generics
2012-08-29 23:57:37 +04:00
2012-09-07 12:37:06 +04:00
class CommentRoot(generics.RootAPIView):
2012-08-29 23:57:37 +04:00
model = Comment
serializer_class = CommentSerializer
2012-09-07 12:37:06 +04:00
class CommentInstance(generics.InstanceAPIView):
2012-08-29 23:57:37 +04:00
model = Comment
serializer_class = CommentSerializer
2012-09-07 12:37:06 +04:00
Wow, that's pretty concise. We've got a huge amount for free, and our code looks like good, clean, idomatic Django.
2012-08-29 23:57:37 +04:00
2012-09-19 16:02:10 +04:00
Next we'll move onto [part 4 of the tutorial][tut-4], 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.
2012-08-29 23:57:37 +04:00
2012-09-19 16:02:10 +04:00
[dry]: http://en.wikipedia.org/wiki/Don't_repeat_yourself
[tut-4]: 4-authentication-permissions-and-throttling.md