diff options
| author | Tom Christie | 2012-09-03 17:49:22 +0100 |
|---|---|---|
| committer | Tom Christie | 2012-09-03 17:49:22 +0100 |
| commit | 6e21915934686cc7d46c8144403c933fa6fd2375 (patch) | |
| tree | a11ccaa21cc735b3223cb6d81796e1ebe6bf84c2 /docs/tutorial | |
| parent | a092a72844705e3129b8996b81d8424997b5d37f (diff) | |
| download | django-rest-framework-6e21915934686cc7d46c8144403c933fa6fd2375.tar.bz2 | |
First pass at mixins & generic views
Diffstat (limited to 'docs/tutorial')
| -rw-r--r-- | docs/tutorial/3-class-based-views.md | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/docs/tutorial/3-class-based-views.md b/docs/tutorial/3-class-based-views.md index 616a6058..21255a68 100644 --- a/docs/tutorial/3-class-based-views.md +++ b/docs/tutorial/3-class-based-views.md @@ -57,7 +57,7 @@ So far, so good. It looks pretty similar to the previous case, but we've got be comment = serializer.deserialized comment.save() return Response(serializer.data) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + return Response(serializer.error_data, status=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk, format=None): comment = self.get_object(pk) @@ -79,27 +79,38 @@ We can compose those mixin classes, to recreate our existing API behaviour with from blog.models import Comment from blog.serializers import CommentSerializer - from djangorestframework import mixins, views + from djangorestframework import mixins + from djangorestframework import views + - class CommentRoot(mixins.ListModelQuerysetMixin, - mixins.CreateModelInstanceMixin, - views.BaseRootAPIView): + class CommentRoot(mixins.ListModelMixin, + mixins.CreateModelMixin, + views.MultipleObjectBaseView): model = Comment serializer_class = CommentSerializer - get = list - post = create + def get(self, request, *args, **kwargs): + return self.list(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + return self.create(request, *args, **kwargs) - class CommentInstance(mixins.RetrieveModelInstanceMixin, - mixins.UpdateModelInstanceMixin, - mixins.DestroyModelInstanceMixin, - views.BaseInstanceAPIView): + + class CommentInstance(mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, + views.SingleObjectBaseView): model = Comment serializer_class = CommentSerializer - get = retrieve - put = update - delete = destroy + 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) ## Reusing generic class based views @@ -109,11 +120,11 @@ That's a lot less code than before, but we can go one step further still. REST from blog.serializers import CommentSerializer from djangorestframework import views - class CommentRoot(views.RootAPIView): + class CommentRoot(views.RootModelView): model = Comment serializer_class = CommentSerializer - class CommentInstance(views.InstanceAPIView): + class CommentInstance(views.InstanceModelView): model = Comment serializer_class = CommentSerializer |
