Skip to content

Commit cba426b

Browse files
Ryan P Kilbycarltongibson
authored andcommitted
Use old url_name behavior in route decorators (#5915)
* Wrap action decorator for old url_name behavior
1 parent 3365ec2 commit cba426b

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

docs/topics/release-notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ You can determine your currently installed version using `pip show`:
8787
* Merged `list_route` and `detail_route` into a single `action` decorator.
8888
* Get all extra actions on a `ViewSet` with `.get_extra_actions()`.
8989
* Extra actions now set the `url_name` and `url_path` on the decorated method.
90+
* `url_name` is now based on the function name, instead of the `url_path`,
91+
as the path is not always suitable (e.g., capturing arguments in the path).
9092
* Enable action url reversing through `.reverse_action()` method (added in 3.7.4)
9193
* Example reverse call: `self.reverse_action(self.custom_action.url_name)`
9294
* Add `detail` initkwarg to indicate if the current action is operating on a
@@ -97,6 +99,8 @@ You can determine your currently installed version using `pip show`:
9799
* Deprecated `list_route` & `detail_route` in favor of `action` decorator with `detail` boolean.
98100
* Deprecated dynamic list/detail route variants in favor of `DynamicRoute` with `detail` boolean.
99101
* Refactored the router's dynamic route generation.
102+
* `list_route` and `detail_route` maintain the old behavior of `url_name`,
103+
basing it on the `url_path` instead of the function name.
100104

101105
* Fix formatting of the 3.7.4 release note [#5704][gh5704]
102106
* Docs: Update DRF Writable Nested Serializers references [#5711][gh5711]

rest_framework/decorators.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ def action(methods=None, detail=None, url_path=None, url_name=None, **kwargs):
147147
def decorator(func):
148148
func.bind_to_methods = methods
149149
func.detail = detail
150-
func.url_path = url_path or func.__name__
151-
func.url_name = url_name or func.__name__.replace('_', '-')
150+
func.url_path = url_path if url_path else func.__name__
151+
func.url_name = url_name if url_name else func.__name__.replace('_', '-')
152152
func.kwargs = kwargs
153153
return func
154154
return decorator
@@ -163,7 +163,13 @@ def detail_route(methods=None, **kwargs):
163163
"`action`, which accepts a `detail` bool. Use `@action(detail=True)` instead.",
164164
PendingDeprecationWarning, stacklevel=2
165165
)
166-
return action(methods, detail=True, **kwargs)
166+
167+
def decorator(func):
168+
func = action(methods, detail=True, **kwargs)(func)
169+
if 'url_name' not in kwargs:
170+
func.url_name = func.url_path.replace('_', '-')
171+
return func
172+
return decorator
167173

168174

169175
def list_route(methods=None, **kwargs):
@@ -175,4 +181,10 @@ def list_route(methods=None, **kwargs):
175181
"`action`, which accepts a `detail` bool. Use `@action(detail=False)` instead.",
176182
PendingDeprecationWarning, stacklevel=2
177183
)
178-
return action(methods, detail=False, **kwargs)
184+
185+
def decorator(func):
186+
func = action(methods, detail=False, **kwargs)(func)
187+
if 'url_name' not in kwargs:
188+
func.url_name = func.url_path.replace('_', '-')
189+
return func
190+
return decorator

tests/test_decorators.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,13 @@ def view(request):
215215
"3.10 in favor of `action`, which accepts a `detail` bool. Use "
216216
"`@action(detail=False)` instead."
217217
)
218+
219+
def test_route_url_name_from_path(self):
220+
# pre-3.8 behavior was to base the `url_name` off of the `url_path`
221+
with pytest.warns(PendingDeprecationWarning):
222+
@list_route(url_path='foo_bar')
223+
def view(request):
224+
pass
225+
226+
assert view.url_path == 'foo_bar'
227+
assert view.url_name == 'foo-bar'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy