2016-07-08 10:38:50 +03:00
# Tutorial 3: Class-based Views
2012-08-29 23:57:37 +04:00
2016-07-08 10:38:50 +03: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
2016-07-08 10:38:50 +03:00
## Rewriting our API using class-based views
2012-08-29 23:57:37 +04:00
2016-07-08 10:38:50 +03:00
We'll start by rewriting the root view as a class-based view. All this involves is a little bit of refactoring of `views.py` .
2012-08-29 23:57:37 +04:00
2012-11-27 18:30:14 +04:00
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
2012-08-29 23:57:37 +04:00
from django.http import Http404
2012-09-20 16:06:27 +04:00
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
2012-09-08 00:53:02 +04:00
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
class SnippetList(APIView):
2012-08-29 23:57:37 +04:00
"""
2012-10-28 23:25:51 +04:00
List all snippets, or create a new snippet.
2012-09-08 00:53:02 +04:00
"""
2012-08-29 23:57:37 +04:00
def get(self, request, format=None):
2012-10-28 23:25:51 +04:00
snippets = Snippet.objects.all()
2013-02-12 12:57:23 +04:00
serializer = SnippetSerializer(snippets, many=True)
2012-08-29 23:57:37 +04:00
return Response(serializer.data)
2012-09-08 00:53:02 +04:00
def post(self, request, format=None):
2014-11-25 14:42:43 +03:00
serializer = SnippetSerializer(data=request.data)
2012-08-29 23:57:37 +04:00
if serializer.is_valid():
2012-10-28 23:25:51 +04:00
serializer.save()
2012-10-03 09:41:03 +04:00
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
2012-09-03 19:42:57 +04:00
2014-08-16 06:45:28 +04:00
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 `views.py` .
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
class SnippetDetail(APIView):
2012-08-29 23:57:37 +04:00
"""
2012-10-28 23:25:51 +04:00
Retrieve, update or delete a snippet instance.
2012-08-29 23:57:37 +04:00
"""
2016-10-20 11:42:40 +03:00
def get_object(self, pk):
2012-08-29 23:57:37 +04:00
try:
2016-10-20 11:42:40 +03:00
return Snippet.objects.get(pk=pk)
2012-10-28 23:25:51 +04:00
except Snippet.DoesNotExist:
2012-08-29 23:57:37 +04:00
raise Http404
2012-09-08 00:53:02 +04:00
2016-10-20 11:42:40 +03:00
def get(self, request, pk, format=None):
snippet = self.get_object(pk)
2012-11-05 14:53:20 +04:00
serializer = SnippetSerializer(snippet)
2012-08-29 23:57:37 +04:00
return Response(serializer.data)
2012-09-08 00:53:02 +04:00
2016-10-20 11:42:40 +03:00
def put(self, request, pk, format=None):
snippet = self.get_object(pk)
2014-11-25 14:42:43 +03:00
serializer = SnippetSerializer(snippet, data=request.data)
2012-08-29 23:57:37 +04:00
if serializer.is_valid():
2012-10-28 23:25:51 +04:00
serializer.save()
2012-08-29 23:57:37 +04:00
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
2016-10-20 11:42:40 +03:00
def delete(self, request, pk, format=None):
snippet = self.get_object(pk)
2012-10-28 23:25:51 +04:00
snippet.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
2017-11-27 13:34:17 +03:00
We'll also need to refactor our `snippets/urls.py` slightly now that we're using class-based views.
2012-09-17 23:19:45 +04:00
2018-05-08 11:06:14 +03:00
from django.urls import path
2012-09-20 16:06:27 +04:00
from rest_framework.urlpatterns import format_suffix_patterns
2012-11-27 18:30:14 +04:00
from snippets import views
2012-09-17 23:19:45 +04:00
2014-09-24 01:08:38 +04:00
urlpatterns = [
2018-05-08 11:06:14 +03:00
path('snippets/', views.SnippetList.as_view()),
path('snippets/< int:pk > /', views.SnippetDetail.as_view()),
2014-09-24 01:08:38 +04:00
]
2014-08-16 06:45:28 +04:00
2012-09-17 23:19:45 +04:00
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
2016-07-08 10:38:50 +03:00
One of the big wins of using class-based views is that it allows us to easily compose reusable bits of behaviour.
2012-08-29 23:57:37 +04:00
2013-08-07 22:00:06 +04:00
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.
2012-08-29 23:57:37 +04:00
2013-10-18 12:32:04 +04:00
Let's take a look at how we can compose the views by using the mixin classes. Here's our `views.py` module again.
2012-08-29 23:57:37 +04:00
2012-11-27 18:30:14 +04:00
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
2012-09-20 16:06:27 +04:00
from rest_framework import mixins
from rest_framework import generics
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
class SnippetList(mixins.ListModelMixin,
2012-09-03 20:49:22 +04:00
mixins.CreateModelMixin,
2013-04-25 01:40:24 +04:00
generics.GenericAPIView):
queryset = Snippet.objects.all()
2012-10-28 23:25:51 +04:00
serializer_class = SnippetSerializer
2012-08-29 23:57:37 +04:00
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
2013-05-28 19:13:12 +04:00
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` .
2012-09-07 12:37:06 +04:00
2012-10-21 18:34:07 +04:00
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.
2012-09-03 20:49:22 +04:00
2012-10-28 23:25:51 +04:00
class SnippetDetail(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
2013-04-25 01:40:24 +04:00
generics.GenericAPIView):
queryset = Snippet.objects.all()
2012-10-28 23:25:51 +04:00
serializer_class = SnippetSerializer
2012-08-29 23:57:37 +04:00
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
2013-04-25 01:40:24 +04:00
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.
2012-09-07 12:37:06 +04:00
2016-07-08 10:38:50 +03:00
## Using generic class-based views
2012-08-29 23:57:37 +04:00
2013-10-18 12:32:04 +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 to trim down our `views.py` module even more.
2012-08-29 23:57:37 +04:00
2012-11-27 18:30:14 +04:00
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
2012-09-20 16:06:27 +04:00
from rest_framework import generics
2012-08-29 23:57:37 +04:00
2012-09-07 12:37:06 +04:00
2012-10-28 23:25:51 +04:00
class SnippetList(generics.ListCreateAPIView):
2013-04-25 01:40:24 +04:00
queryset = Snippet.objects.all()
2012-10-28 23:25:51 +04:00
serializer_class = SnippetSerializer
2012-08-29 23:57:37 +04:00
2012-09-07 12:37:06 +04:00
2012-10-28 23:25:51 +04:00
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
2013-04-25 01:40:24 +04:00
queryset = Snippet.objects.all()
2012-10-28 23:25:51 +04:00
serializer_class = SnippetSerializer
2012-08-29 23:57:37 +04:00
2012-12-05 19:08:13 +04:00
Wow, that's pretty concise. We've gotten a huge amount for free, and our code looks like good, clean, idiomatic Django.
2012-08-29 23:57:37 +04:00
2012-10-28 23:25:51 +04:00
Next we'll move onto [part 4 of the tutorial][tut-4], where we'll take a look at how we can deal with authentication and permissions for our API.
2012-08-29 23:57:37 +04:00
2018-01-08 18:22:32 +03:00
[dry]: https://en.wikipedia.org/wiki/Don't_repeat_yourself
2012-10-28 23:25:51 +04:00
[tut-4]: 4-authentication-and-permissions.md