Skip to content

Commit 6aa0e30

Browse files
committed
Added documentation about url_path parameter for custom actions.
1 parent a8a3fed commit 6aa0e30

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/api-guide/routers.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ The following URL pattern would additionally be generated:
6868

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

71+
If you did not like the default URL generated for your custom action, you could use `url_path` parameter with `@detail_route` or `@list_route` to customize it.
72+
73+
For example, if you want to change the URL for our custom action to `^users/{pk}/change-password/$`, you could write:
74+
75+
from myapp.permissions import IsAdminOrIsSelf
76+
from rest_framework.decorators import detail_route
77+
78+
class UserViewSet(ModelViewSet):
79+
...
80+
81+
@detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf], url_path='change-password')
82+
def set_password(self, request, pk=None):
83+
...
84+
85+
Above example would instead generate following URL pattern:
86+
87+
* URL pattern: `^users/{pk}/change-password/$` Name: `'user-change-password'`
88+
7189
For more information see the viewset documentation on [marking extra actions for routing][route-decorators].
7290

7391
# API Guide

0 commit comments

Comments
 (0)