Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/api-guide/routers.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ The above example would now generate the following URL pattern:

* URL pattern: `^users/{pk}/change-password/$` Name: `'user-change-password'`

In the case you do not want to use the default name generated for your custom action, you can use the url_name parameter to customize it.

For example, if you want to change the name of our custom action to `'user-change-password'`, you could write:

from myapp.permissions import IsAdminOrIsSelf
from rest_framework.decorators import detail_route

class UserViewSet(ModelViewSet):
...

@detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf], url_name='change-password')
def set_password(self, request, pk=None):
...

The above example would now generate the following URL pattern:

* URL pattern: `^users/{pk}/set_password/$` Name: `'user-change-password'`

You can also use url_path and url_name parameters together to obtain extra control on URL generation for custom views.

For more information see the viewset documentation on [marking extra actions for routing][route-decorators].

# API Guide
Expand Down
3 changes: 2 additions & 1 deletion rest_framework/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ def _get_dynamic_routes(route, dynamic_routes):
initkwargs = route.initkwargs.copy()
initkwargs.update(method_kwargs)
url_path = initkwargs.pop("url_path", None) or methodname
url_name = initkwargs.pop("url_name", None) or url_path
ret.append(Route(
url=replace_methodname(route.url, url_path),
mapping={httpmethod: methodname for httpmethod in httpmethods},
name=replace_methodname(route.name, url_path),
name=replace_methodname(route.name, url_name),
initkwargs=initkwargs,
))

Expand Down