mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 19:40:13 +03:00
Added has_lookup_field argument to the link and action decorators.
This commit is contained in:
parent
3765865b4b
commit
ea9e78caf5
|
@ -142,9 +142,16 @@ The `@action` decorator will route `POST` requests by default, but may also acce
|
||||||
@action(methods=['POST', 'DELETE'])
|
@action(methods=['POST', 'DELETE'])
|
||||||
def unset_password(self, request, pk=None):
|
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 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):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -107,22 +107,24 @@ def permission_classes(permission_classes):
|
||||||
return decorator
|
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.
|
Used to mark a method on a ViewSet that should be routed for GET requests.
|
||||||
"""
|
"""
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
|
func.has_lookup_field = has_lookup_field
|
||||||
func.bind_to_methods = ['get']
|
func.bind_to_methods = ['get']
|
||||||
func.kwargs = kwargs
|
func.kwargs = kwargs
|
||||||
return func
|
return func
|
||||||
return decorator
|
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.
|
Used to mark a method on a ViewSet that should be routed for POST requests.
|
||||||
"""
|
"""
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
|
func.has_lookup_field = has_lookup_field
|
||||||
func.bind_to_methods = methods
|
func.bind_to_methods = methods
|
||||||
func.kwargs = kwargs
|
func.kwargs = kwargs
|
||||||
return func
|
return func
|
||||||
|
|
|
@ -110,6 +110,14 @@ class SimpleRouter(BaseRouter):
|
||||||
name='{basename}-{methodnamehyphen}',
|
name='{basename}-{methodnamehyphen}',
|
||||||
initkwargs={}
|
initkwargs={}
|
||||||
),
|
),
|
||||||
|
Route(
|
||||||
|
url=r'^{prefix}/{methodname}{trailing_slash}$',
|
||||||
|
mapping={
|
||||||
|
'{httpmethod}': '{methodname}',
|
||||||
|
},
|
||||||
|
name='{basename}-{methodnamehyphen}',
|
||||||
|
initkwargs={}
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, trailing_slash=True):
|
def __init__(self, trailing_slash=True):
|
||||||
|
@ -146,18 +154,23 @@ class SimpleRouter(BaseRouter):
|
||||||
for methodname in dir(viewset):
|
for methodname in dir(viewset):
|
||||||
attr = getattr(viewset, methodname)
|
attr = getattr(viewset, methodname)
|
||||||
httpmethods = getattr(attr, 'bind_to_methods', None)
|
httpmethods = getattr(attr, 'bind_to_methods', None)
|
||||||
if httpmethods:
|
has_lookup_field = getattr(attr, 'has_lookup_field', True)
|
||||||
if methodname in known_actions:
|
if not httpmethods:
|
||||||
raise ImproperlyConfigured('Cannot use @action or @link decorator on '
|
continue
|
||||||
'method "%s" as it is an existing route' % methodname)
|
if methodname in known_actions:
|
||||||
httpmethods = [method.lower() for method in httpmethods]
|
raise ImproperlyConfigured('Cannot use @action or @link decorator on '
|
||||||
dynamic_routes.append((httpmethods, methodname))
|
'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 = []
|
ret = []
|
||||||
for route in self.routes:
|
for route in self.routes:
|
||||||
if route.mapping == {'{httpmethod}': '{methodname}'}:
|
if route.mapping == {'{httpmethod}': '{methodname}'}:
|
||||||
# Dynamic routes (@link or @action decorator)
|
# 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 = route.initkwargs.copy()
|
||||||
initkwargs.update(getattr(viewset, methodname).kwargs)
|
initkwargs.update(getattr(viewset, methodname).kwargs)
|
||||||
ret.append(Route(
|
ret.append(Route(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user