@@ -72,14 +72,32 @@ Alternatively you can use Django's `include` function, like so…
72
72
url(r'^', include(router.urls)),
73
73
]
74
74
75
- Router URL patterns can also be namespaces.
75
+ You may use ` include ` with an application namespace:
76
76
77
77
urlpatterns = [
78
78
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') )),
80
80
]
81
81
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
+ ---
83
101
84
102
### Routing for extra actions
85
103
@@ -315,3 +333,5 @@ The [`DRF-extensions` package][drf-extensions] provides [routers][drf-extensions
315
333
[ drf-extensions-nested-viewsets ] : https://chibisov.github.io/drf-extensions/docs/#nested-routes
316
334
[ drf-extensions-collection-level-controllers ] : https://chibisov.github.io/drf-extensions/docs/#collection-level-controllers
317
335
[ 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