From 50c6bc5762460ebd2a79b61edd534d10cb58c7e5 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 26 Apr 2013 13:31:19 +0100 Subject: [PATCH] Fix up viewset docs slightly --- docs/api-guide/viewsets.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/api-guide/viewsets.md b/docs/api-guide/viewsets.md index 1bca491dd..36a4dbd5c 100644 --- a/docs/api-guide/viewsets.md +++ b/docs/api-guide/viewsets.md @@ -19,7 +19,7 @@ Typically, rather than exlicitly registering the views in a viewset in the urlco Let's define a simple viewset that can be used to listing or retrieving all the users in the system. - class UserViewSet(ViewSet): + class UserViewSet(viewsets.ViewSet): """ A simple ViewSet that for listing or retrieving users. """ @@ -45,6 +45,15 @@ Typically we wouldn't do this, but would instead register the viewset with a rou router.register(r'users', UserViewSet, 'user') urlpatterns = router.urls +Rather than writing your own viewsets, you'll often want to use the existing base classes that provide a default set of behavior. For example: + + class UserViewSet(viewsets.ModelViewSet): + """ + A viewset for viewing and editing user instances. + """ + serializer_class = UserSerializer + queryset = User.objects.all() + There are two main advantages of using a `ViewSet` class over using a `View` class. * Repeated logic can be combined into a single class. In the above example, we only need to specify the `queryset` once, and it'll be used across multiple views. @@ -60,6 +69,9 @@ The default routers included with REST framework will provide routes for a stand """ Example empty viewset demonstrating the standard actions that will be handled by a router class. + + If you're using format suffixes, make sure to also include + the `format=None` keyword argument for each action. """ def list(self, request): @@ -197,4 +209,4 @@ For example, we can create a base viewset class that provides `retrieve`, `updat By creating your own base `ViewSet` classes, you can provide common behavior that can be reused in multiple views across your API. -[cite]: http://guides.rubyonrails.org/routing.html \ No newline at end of file +[cite]: http://guides.rubyonrails.org/routing.html