diff --git a/docs/api-guide/viewsets.md b/docs/api-guide/viewsets.md index 1062cb32c..12c9cd930 100644 --- a/docs/api-guide/viewsets.md +++ b/docs/api-guide/viewsets.md @@ -142,9 +142,16 @@ The `@action` decorator will route `POST` requests by default, but may also acce @action(methods=['POST', 'DELETE']) def unset_password(self, request, pk=None): ... - + The two new actions will then be available at the urls `^users/{pk}/set_password/$` and `^users/{pk}/unset_password/$` +The `@action` and `@link` decorators will add `{pk}` to the route by default, by using the `has_lookup_field` argument this behavior can be disabled. +*The `has_lookup_field` argument has no effect when using `ModelViewSet` or `ReadOnlyModelViewSet`* + + @action(has_lookup_field=False) + def others(self, request): + ... + --- diff --git a/rest_framework/decorators.py b/rest_framework/decorators.py index c69756a43..90647307b 100644 --- a/rest_framework/decorators.py +++ b/rest_framework/decorators.py @@ -107,22 +107,24 @@ def permission_classes(permission_classes): return decorator -def link(**kwargs): +def link(has_lookup_field=True, **kwargs): """ Used to mark a method on a ViewSet that should be routed for GET requests. """ def decorator(func): + func.has_lookup_field = has_lookup_field func.bind_to_methods = ['get'] func.kwargs = kwargs return func return decorator -def action(methods=['post'], **kwargs): +def action(methods=['post'], has_lookup_field=True, **kwargs): """ Used to mark a method on a ViewSet that should be routed for POST requests. """ def decorator(func): + func.has_lookup_field = has_lookup_field func.bind_to_methods = methods func.kwargs = kwargs return func diff --git a/rest_framework/routers.py b/rest_framework/routers.py index 3fee1e494..ac76885a8 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -110,6 +110,14 @@ class SimpleRouter(BaseRouter): name='{basename}-{methodnamehyphen}', initkwargs={} ), + Route( + url=r'^{prefix}/{methodname}{trailing_slash}$', + mapping={ + '{httpmethod}': '{methodname}', + }, + name='{basename}-{methodnamehyphen}', + initkwargs={} + ), ] def __init__(self, trailing_slash=True): @@ -146,18 +154,23 @@ class SimpleRouter(BaseRouter): for methodname in dir(viewset): attr = getattr(viewset, methodname) httpmethods = getattr(attr, 'bind_to_methods', None) - if httpmethods: - if methodname in known_actions: - raise ImproperlyConfigured('Cannot use @action or @link decorator on ' - 'method "%s" as it is an existing route' % methodname) - httpmethods = [method.lower() for method in httpmethods] - dynamic_routes.append((httpmethods, methodname)) + has_lookup_field = getattr(attr, 'has_lookup_field', True) + if not httpmethods: + continue + if methodname in known_actions: + raise ImproperlyConfigured('Cannot use @action or @link decorator on ' + 'method "%s" as it is an existing route' % methodname) + httpmethods = [method.lower() for method in httpmethods] + dynamic_routes.append((httpmethods, methodname, has_lookup_field)) ret = [] for route in self.routes: if route.mapping == {'{httpmethod}': '{methodname}'}: # Dynamic routes (@link or @action decorator) - for httpmethods, methodname in dynamic_routes: + is_lookup_route = '{lookup}' in route.url + for httpmethods, methodname, has_lookup_field in dynamic_routes: + if has_lookup_field != is_lookup_route: + continue initkwargs = route.initkwargs.copy() initkwargs.update(getattr(viewset, methodname).kwargs) ret.append(Route(