Commit 50c6bc57 by Tom Christie

Fix up viewset docs slightly

parent 51f80c36
...@@ -19,7 +19,7 @@ Typically, rather than exlicitly registering the views in a viewset in the urlco ...@@ -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. 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. 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 ...@@ -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') router.register(r'users', UserViewSet, 'user')
urlpatterns = router.urls 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. 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. * 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 ...@@ -60,6 +69,9 @@ The default routers included with REST framework will provide routes for a stand
""" """
Example empty viewset demonstrating the standard Example empty viewset demonstrating the standard
actions that will be handled by a router class. 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): def list(self, request):
...@@ -197,4 +209,4 @@ For example, we can create a base viewset class that provides `retrieve`, `updat ...@@ -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. 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 [cite]: http://guides.rubyonrails.org/routing.html
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment