Skip to content

Commit aaf55ac

Browse files
carltongibsonPierre Chiquet
authored andcommitted
Corrected docs on router include with namespaces. (encode#5843)
* Provide both app and instance namespace examples * Emphasise non-namespaced option
1 parent af15743 commit aaf55ac

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

docs/api-guide/routers.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,32 @@ Alternatively you can use Django's `include` function, like so…
7272
url(r'^', include(router.urls)),
7373
]
7474

75-
Router URL patterns can also be namespaces.
75+
You may use `include` with an application namespace:
7676

7777
urlpatterns = [
7878
url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
79-
url(r'^api/', include(router.urls, namespace='api')),
79+
url(r'^api/', include((router.urls, 'app_name'))),
8080
]
8181

82-
If using namespacing with hyperlinked serializers you'll also need to ensure that any `view_name` parameters on the serializers correctly reflect the namespace. In the example above you'd need to include a parameter such as `view_name='api:user-detail'` for serializer fields hyperlinked to the user detail view.
82+
Or both an application and instance namespace:
83+
84+
urlpatterns = [
85+
url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
86+
url(r'^api/', include((router.urls, 'app_name'), namespace='instance_name')),
87+
]
88+
89+
See Django's [URL namespaces docs][url-namespace-docs] and the [`include` API reference][include-api-reference] for more details.
90+
91+
---
92+
93+
**Note**: If using namespacing with hyperlinked serializers you'll also need to ensure that any `view_name` parameters
94+
on the serializers correctly reflect the namespace. In the examples above you'd need to include a parameter such as
95+
`view_name='app_name:user-detail'` for serializer fields hyperlinked to the user detail view.
96+
97+
The automatic `view_name` generation uses a pattern like `%(model_name)-detail`. Unless your models names actually clash
98+
you may be better off **not** namespacing your Django REST Framework views when using hyperlinked serializers.
99+
100+
---
83101

84102
### Routing for extra actions
85103

@@ -315,3 +333,5 @@ The [`DRF-extensions` package][drf-extensions] provides [routers][drf-extensions
315333
[drf-extensions-nested-viewsets]: https://chibisov.github.io/drf-extensions/docs/#nested-routes
316334
[drf-extensions-collection-level-controllers]: https://chibisov.github.io/drf-extensions/docs/#collection-level-controllers
317335
[drf-extensions-customizable-endpoint-names]: https://chibisov.github.io/drf-extensions/docs/#controller-endpoint-name
336+
[url-namespace-docs]: https://docs.djangoproject.com/en/1.11/topics/http/urls/#url-namespaces
337+
[include-api-reference]: https://docs.djangoproject.com/en/2.0/ref/urls/#include

0 commit comments

Comments
 (0)