django rest framework 入门3——Class Based Views
2013-02-26 13:46
645 查看
转自:http://django-rest-framework.org/tutorial/3-class-based-views.html
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.
We'll start by rewriting the root view as a class based view. All this involves is a little bit of refactoring.
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.
That's looking good. Again, it's still pretty similar to the function based view right now.
We'll also need to refactor our URLconf slightly now we're using class based views.
Okay, we're done. If you run the development server everything should be working just as before.
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 simliar 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 our views by using the mixin classes.
We'll take a moment to examine exactly what's happening here. We're building our view using
and adding in
The base class provides the core functionality, and the mixin classes provide the
We're then explicitly binding the
to the appropriate actions. Simple enough stuff so far.
Pretty similar. This time we're using the
to provide the core functionality, and adding in mixins to provide the
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.
Wow, that's pretty concise. We've gotten a huge amount for free, and our code looks like good, clean, idiomatic Django.
Next we'll move onto part 4 of the tutorial, where we'll take a look at how we can deal with authentication and permissions
for our API.
Tutorial 3: Class Based Views
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.
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 snippets.models import Snippet from snippets.serializers import SnippetSerializer from django.http import Http404 from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status class SnippetList(APIView): """ List all snippets, or create a new snippet. """ def get(self, request, format=None): snippets = Snippet.objects.all() serializer = SnippetSerializer(snippets, many=True) return Response(serializer.data) def post(self, request, format=None): serializer = SnippetSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
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.
class SnippetDetail(APIView): """ Retrieve, update or delete a snippet instance. """ def get_object(self, pk): try: return Snippet.objects.get(pk=pk) except Snippet.DoesNotExist: raise Http404 def get(self, request, pk, format=None): snippet = self.get_object(pk) serializer = SnippetSerializer(snippet) return Response(serializer.data) def put(self, request, pk, format=None): snippet = self.get_object(pk) serializer = SnippetSerializer(snippet, data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk, format=None): snippet = self.get_object(pk) snippet.delete() return Response(status=status.HTTP_204_NO_CONTENT)
That's looking good. Again, it's still pretty similar to the function based view right now.
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 from snippets import views urlpatterns = patterns('', url(r'^snippets/$', views.SnippetList.as_view()), url(r'^snippets/(?P<pk>[0-9]+)/$', views.SnippetDetail.as_view()), ) urlpatterns = format_suffix_patterns(urlpatterns)
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.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.
Let's take a look at how we can compose our views by using the mixin classes.
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.MultipleObjectAPIView): model = Snippet 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)
We'll take a moment to examine exactly what's happening here. We're building our view using
MultipleObjectAPIView,
and adding in
ListModelMixinand
CreateModelMixin.
The base class provides the core functionality, and the mixin classes provide the
.list()and
.create()actions.
We're then explicitly binding the
getand
postmethods
to the appropriate actions. Simple enough stuff so far.
class SnippetDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.SingleObjectAPIView): model = Snippet serializer_class = SnippetSerializer 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)
Pretty similar. This time we're using the
SingleObjectAPIViewclass
to provide the core functionality, and adding in mixins to provide the
.retrieve(),
.update()and
.destroy()actions.
Using generic class based views
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.from snippets.models import Snippet from snippets.serializers import SnippetSerializer from rest_framework import generics class SnippetList(generics.ListCreateAPIView): model = Snippet serializer_class = SnippetSerializer class SnippetDetail(generics.RetrieveUpdateDestroyAPIView): model = Snippet serializer_class = SnippetSerializer
Wow, that's pretty concise. We've gotten a huge amount for free, and our code looks like good, clean, idiomatic Django.
Next we'll move onto part 4 of the tutorial, where we'll take a look at how we can deal with authentication and permissions
for our API.
相关文章推荐
- Django-Rest-Framework 教程: 3. 使用 class based views
- django class-based-views
- Django Class-based generic views 基于类的通用视图
- 基于类的通用视图(Class-based generic views)
- 用基于类的通用视图处理表单(Class-based generic views)
- django rest framework 入门2——Request and Response
- 用基于类的通用视图处理表单(Class-based generic views)
- python django rest framework Class-based Views
- Django REST framework 的快速入门教程
- django rest framework 入门1-序列化 Serialization
- Django入门3:视图views
- Django REST framework-API指南04-Generic views 原创翻译
- django rest framework 入门1-序列化 Serialization
- python django rest framework 快速入门
- Django REST framework之views.py
- Django学习笔记之Class-Based-View
- [py][mx]django使用class写views-免去判断方法的烦恼
- Django class based view walkthrough: TemplateView
- django rest framework 入门4——Authentication & Permissions
- 121、Django rest framework入门使用