Skip to content

Commit fd7d3a7

Browse files
geryogamauvipy
andauthored
Update 6-viewsets-and-routers.md (#8590)
* Update 6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md * Update docs/tutorial/6-viewsets-and-routers.md Co-authored-by: Asif Saif Uddin <[email protected]>
1 parent 8175f05 commit fd7d3a7

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

docs/tutorial/6-viewsets-and-routers.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ A `ViewSet` class is only bound to a set of method handlers at the last moment,
1010

1111
Let's take our current set of views, and refactor them into view sets.
1212

13-
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:
13+
First of all let's refactor our `UserList` and `UserDetail` classes into a single `UserViewSet` class. We can remove the two view classes, and replace them with a single ViewSet class:
1414

1515
from rest_framework import viewsets
1616

17+
1718
class UserViewSet(viewsets.ReadOnlyModelViewSet):
1819
"""
1920
This viewset automatically provides `list` and `retrieve` actions.
@@ -25,13 +26,14 @@ Here we've used the `ReadOnlyModelViewSet` class to automatically provide the de
2526

2627
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.
2728

29+
from rest_framework import permissions
2830
from rest_framework.decorators import action
2931
from rest_framework.response import Response
30-
from rest_framework import permissions
32+
3133

3234
class SnippetViewSet(viewsets.ModelViewSet):
3335
"""
34-
This viewset automatically provides `list`, `create`, `retrieve`,
36+
This ViewSet automatically provides `list`, `create`, `retrieve`,
3537
`update` and `destroy` actions.
3638

3739
Additionally we also provide an extra `highlight` action.
@@ -64,9 +66,10 @@ To see what's going on under the hood let's first explicitly create a set of vie
6466

6567
In the `snippets/urls.py` file we bind our `ViewSet` classes into a set of concrete views.
6668

67-
from snippets.views import SnippetViewSet, UserViewSet, api_root
6869
from rest_framework import renderers
6970

71+
from snippets.views import api_root, SnippetViewSet, UserViewSet
72+
7073
snippet_list = SnippetViewSet.as_view({
7174
'get': 'list',
7275
'post': 'create'
@@ -87,7 +90,7 @@ In the `snippets/urls.py` file we bind our `ViewSet` classes into a set of concr
8790
'get': 'retrieve'
8891
})
8992

90-
Notice how we're creating multiple views from each `ViewSet` class, by binding the http methods to the required action for each view.
93+
Notice how we're creating multiple views from each `ViewSet` class, by binding the HTTP methods to the required action for each view.
9194

9295
Now that we've bound our resources into concrete views, we can register the views with the URL conf as usual.
9396

@@ -108,9 +111,10 @@ Here's our re-wired `snippets/urls.py` file.
108111

109112
from django.urls import path, include
110113
from rest_framework.routers import DefaultRouter
114+
111115
from snippets import views
112116

113-
# Create a router and register our viewsets with it.
117+
# Create a router and register our ViewSets with it.
114118
router = DefaultRouter()
115119
router.register(r'snippets', views.SnippetViewSet, basename='snippet')
116120
router.register(r'users', views.UserViewSet, basename='user')
@@ -120,12 +124,12 @@ Here's our re-wired `snippets/urls.py` file.
120124
path('', include(router.urls)),
121125
]
122126

123-
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.
127+
Registering the ViewSets with the router is similar to providing a urlpattern. We include two arguments - the URL prefix for the views, and the view set itself.
124128

125-
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.
129+
The `DefaultRouter` class we're using also automatically creates the API root view for us, so we can now delete the `api_root` function from our `views` module.
126130

127-
## Trade-offs between views vs viewsets
131+
## Trade-offs between views vs ViewSets
128132

129-
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.
133+
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.
130134

131-
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.
135+
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 API views individually.

0 commit comments

Comments
 (0)