django-rest-framework/docs/tutorial/6-viewsets-and-routers.md

157 lines
7.9 KiB
Markdown
Raw Normal View History

2013-05-20 17:02:19 +04:00
# Tutorial 6: ViewSets & Routers
2013-04-25 01:40:24 +04:00
REST framework includes an abstraction for dealing with `ViewSets`, that allows the developer to concentrate on modeling the state and interactions of the API, and leave the URL construction to be handled automatically, based on common conventions.
2013-03-30 21:22:52 +04:00
`ViewSet` classes are almost the same thing as `View` classes, except that they provide operations such as `read`, or `update`, and not method handlers such as `get` or `put`.
2012-08-29 23:57:37 +04:00
2013-03-30 21:22:52 +04:00
A `ViewSet` class is only bound to a set of method handlers at the last moment, when it is instantiated into a set of views, typically by using a `Router` class which handles the complexities of defining the URL conf for you.
2012-08-29 23:57:37 +04:00
2013-03-30 21:22:52 +04:00
## Refactoring to use ViewSets
2012-08-29 23:57:37 +04:00
2013-03-30 21:22:52 +04:00
Let's take our current set of views, and refactor them into view sets.
2013-08-13 11:19:40 +04:00
First of all let's refactor our `UserList` and `UserDetail` views into a single `UserViewSet`. We can remove the two views, and replace them with a single class:
from rest_framework import viewsets
2012-08-29 23:57:37 +04:00
class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
2013-03-30 21:22:52 +04:00
queryset = User.objects.all()
2013-01-15 21:50:39 +04:00
serializer_class = UserSerializer
2014-05-06 15:00:41 +04:00
Here we've used the `ReadOnlyModelViewSet` class to automatically provide the default 'read-only' operations. We're still setting the `queryset` and `serializer_class` attributes exactly as we did when we were using regular views, but we no longer need to provide the same information to two separate classes.
2013-04-05 01:24:30 +04:00
2013-03-30 21:22:52 +04:00
Next we're going to replace the `SnippetList`, `SnippetDetail` and `SnippetHighlight` view classes. We can remove the three views, and again replace them with a single class.
2013-01-15 21:50:39 +04:00
from rest_framework.decorators import detail_route
2013-03-30 21:22:52 +04:00
class SnippetViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
Additionally we also provide an extra `highlight` action.
"""
2013-03-30 21:22:52 +04:00
queryset = Snippet.objects.all()
2013-01-15 21:50:39 +04:00
serializer_class = SnippetSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly,)
@detail_route(renderer_classes=[renderers.StaticHTMLRenderer])
2013-01-15 21:50:39 +04:00
def highlight(self, request, *args, **kwargs):
snippet = self.get_object()
return Response(snippet.highlighted)
def perform_create(self, serializer):
2015-09-04 12:07:20 +03:00
serializer.save(owner=self.request.user)
2013-01-15 21:50:39 +04:00
2013-04-05 01:24:30 +04:00
This time we've used the `ModelViewSet` class in order to get the complete set of default read and write operations.
Notice that we've also used the `@detail_route` decorator to create a custom action, named `highlight`. This decorator can be used to add any custom endpoints that don't fit into the standard `create`/`update`/`delete` style.
2012-08-29 23:57:37 +04:00
Custom actions which use the `@detail_route` decorator will respond to `GET` requests. We can use the `methods` argument if we wanted an action that responded to `POST` requests.
The URLs for custom actions by default depend on the method name itself. If you want to change the way url should be constructed, you can include url_path as a decorator keyword argument.
2013-03-30 21:22:52 +04:00
## Binding ViewSets to URLs explicitly
2013-01-15 21:50:39 +04:00
The handler methods only get bound to the actions when we define the URLConf.
2013-03-30 21:22:52 +04:00
To see what's going on under the hood let's first explicitly create a set of views from our ViewSets.
2013-01-15 21:50:39 +04:00
2013-04-05 01:24:30 +04:00
In the `urls.py` file we bind our `ViewSet` classes into a set of concrete views.
2013-01-15 21:50:39 +04:00
from snippets.views import SnippetViewSet, UserViewSet, api_root
from rest_framework import renderers
2013-01-17 16:30:28 +04:00
2013-04-05 01:24:30 +04:00
snippet_list = SnippetViewSet.as_view({
'get': 'list',
'post': 'create'
})
snippet_detail = SnippetViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
})
snippet_highlight = SnippetViewSet.as_view({
'get': 'highlight'
}, renderer_classes=[renderers.StaticHTMLRenderer])
2013-04-05 01:24:30 +04:00
user_list = UserViewSet.as_view({
'get': 'list'
})
user_detail = UserViewSet.as_view({
'get': 'retrieve'
})
2013-01-16 01:49:24 +04:00
2013-03-30 21:22:52 +04:00
Notice how we're creating multiple views from each `ViewSet` class, by binding the http methods to the required action for each view.
2013-01-17 16:30:28 +04:00
2014-05-06 17:06:38 +04:00
Now that we've bound our resources into concrete views, we can register the views with the URL conf as usual.
2013-01-15 21:50:39 +04:00
urlpatterns = format_suffix_patterns([
url(r'^$', api_root),
2013-01-17 16:30:28 +04:00
url(r'^snippets/$', snippet_list, name='snippet-list'),
url(r'^snippets/(?P<pk>[0-9]+)/$', snippet_detail, name='snippet-detail'),
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
url(r'^users/$', user_list, name='user-list'),
url(r'^users/(?P<pk>[0-9]+)/$', user_detail, name='user-detail')
])
## Using Routers
2012-08-29 23:57:37 +04:00
2013-04-05 01:24:30 +04:00
Because we're using `ViewSet` classes rather than `View` classes, we actually don't need to design the URL conf ourselves. The conventions for wiring up resources into views and urls can be handled automatically, using a `Router` class. All we need to do is register the appropriate view sets with a router, and let it do the rest.
2013-01-17 16:30:28 +04:00
Here's our re-wired `urls.py` file.
2012-08-29 23:57:37 +04:00
from django.conf.urls import url, include
2013-03-30 21:22:52 +04:00
from snippets import views
from rest_framework.routers import DefaultRouter
2012-08-29 23:57:37 +04:00
2013-04-25 01:40:24 +04:00
# Create a router and register our viewsets with it.
2013-01-16 01:49:24 +04:00
router = DefaultRouter()
2013-05-02 15:08:05 +04:00
router.register(r'snippets', views.SnippetViewSet)
router.register(r'users', views.UserViewSet)
2013-04-25 01:40:24 +04:00
# The API URLs are now determined automatically by the router.
2014-11-29 22:23:55 +03:00
# Additionally, we include the login URLs for the browsable API.
urlpatterns = [
2013-04-25 01:40:24 +04:00
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
2013-04-25 01:40:24 +04:00
2013-05-02 15:08:05 +04:00
Registering the viewsets with the router is similar to providing a urlpattern. We include two arguments - the URL prefix for the views, and the viewset itself.
2013-04-25 01:40:24 +04:00
The `DefaultRouter` class we're using also automatically creates the API root view for us, so we can now delete the `api_root` method from our `views` module.
2012-08-29 23:57:37 +04:00
2013-05-11 00:56:33 +04:00
## Trade-offs between views vs viewsets
2012-08-29 23:57:37 +04:00
2013-04-25 01:40:24 +04:00
Using viewsets can be a really useful abstraction. It helps ensure that URL conventions will be consistent across your API, minimizes the amount of code you need to write, and allows you to concentrate on the interactions and representations your API provides rather than the specifics of the URL conf.
2012-08-29 23:57:37 +04:00
2013-05-02 15:08:05 +04:00
That doesn't mean it's always the right approach to take. There's a similar set of trade-offs to consider as when using class-based views instead of function based views. Using viewsets is less explicit than building your views individually.
2012-09-19 16:02:10 +04:00
2013-04-25 01:40:24 +04:00
## Reviewing our work
2014-11-29 22:23:55 +03:00
With an incredibly small amount of code, we've now got a complete pastebin Web API, which is fully web browsable, and comes complete with authentication, per-object permissions, and multiple renderer formats.
2013-04-25 01:40:24 +04:00
We've walked through each step of the design process, and seen how if we need to customize anything we can gradually work our way down to simply using regular Django views.
You can review the final [tutorial code][repo] on GitHub, or try out a live example in [the sandbox][sandbox].
2013-04-25 01:40:24 +04:00
## Onwards and upwards
2014-05-06 17:07:40 +04:00
We've reached the end of our tutorial. If you want to get more involved in the REST framework project, here are a few places you can start:
2013-04-25 01:40:24 +04:00
* Contribute on [GitHub][github] by reviewing and submitting issues, and making pull requests.
* Join the [REST framework discussion group][group], and help build the community.
* Follow [the author][twitter] on Twitter and say hi.
**Now go build awesome things.**
[repo]: https://github.com/tomchristie/rest-framework-tutorial
[sandbox]: http://restframework.herokuapp.com/
[github]: https://github.com/tomchristie/django-rest-framework
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
[twitter]: https://twitter.com/_tomchristie