mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-28 17:33:15 +03:00
Ability to override default method names by customizing it
This commit is contained in:
parent
96bb77dc35
commit
d972df7c9c
|
@ -176,23 +176,27 @@ class SimpleRouter(BaseRouter):
|
||||||
if isinstance(route, DynamicDetailRoute):
|
if isinstance(route, DynamicDetailRoute):
|
||||||
# Dynamic detail routes (@detail_route decorator)
|
# Dynamic detail routes (@detail_route decorator)
|
||||||
for httpmethods, methodname in detail_routes:
|
for httpmethods, methodname in detail_routes:
|
||||||
|
method_kwargs = getattr(viewset, methodname).kwargs
|
||||||
|
custom_method_name = method_kwargs.pop("custom_method_name", None) or methodname
|
||||||
initkwargs = route.initkwargs.copy()
|
initkwargs = route.initkwargs.copy()
|
||||||
initkwargs.update(getattr(viewset, methodname).kwargs)
|
initkwargs.update(method_kwargs)
|
||||||
ret.append(Route(
|
ret.append(Route(
|
||||||
url=replace_methodname(route.url, methodname),
|
url=replace_methodname(route.url, custom_method_name),
|
||||||
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
|
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
|
||||||
name=replace_methodname(route.name, methodname),
|
name=replace_methodname(route.name, custom_method_name),
|
||||||
initkwargs=initkwargs,
|
initkwargs=initkwargs,
|
||||||
))
|
))
|
||||||
elif isinstance(route, DynamicListRoute):
|
elif isinstance(route, DynamicListRoute):
|
||||||
# Dynamic list routes (@list_route decorator)
|
# Dynamic list routes (@list_route decorator)
|
||||||
for httpmethods, methodname in list_routes:
|
for httpmethods, methodname in list_routes:
|
||||||
|
method_kwargs = getattr(viewset, methodname).kwargs
|
||||||
|
custom_method_name = method_kwargs.pop("custom_method_name", None) or methodname
|
||||||
initkwargs = route.initkwargs.copy()
|
initkwargs = route.initkwargs.copy()
|
||||||
initkwargs.update(getattr(viewset, methodname).kwargs)
|
initkwargs.update(method_kwargs)
|
||||||
ret.append(Route(
|
ret.append(Route(
|
||||||
url=replace_methodname(route.url, methodname),
|
url=replace_methodname(route.url, custom_method_name),
|
||||||
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
|
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
|
||||||
name=replace_methodname(route.name, methodname),
|
name=replace_methodname(route.name, custom_method_name),
|
||||||
initkwargs=initkwargs,
|
initkwargs=initkwargs,
|
||||||
))
|
))
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user