mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Use old url_name behavior in route decorators (#5915)
* Wrap action decorator for old url_name behavior
This commit is contained in:
parent
3365ec23ec
commit
cba426b34c
|
@ -87,6 +87,8 @@ You can determine your currently installed version using `pip show`:
|
|||
* Merged `list_route` and `detail_route` into a single `action` decorator.
|
||||
* Get all extra actions on a `ViewSet` with `.get_extra_actions()`.
|
||||
* Extra actions now set the `url_name` and `url_path` on the decorated method.
|
||||
* `url_name` is now based on the function name, instead of the `url_path`,
|
||||
as the path is not always suitable (e.g., capturing arguments in the path).
|
||||
* Enable action url reversing through `.reverse_action()` method (added in 3.7.4)
|
||||
* Example reverse call: `self.reverse_action(self.custom_action.url_name)`
|
||||
* 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`:
|
|||
* Deprecated `list_route` & `detail_route` in favor of `action` decorator with `detail` boolean.
|
||||
* Deprecated dynamic list/detail route variants in favor of `DynamicRoute` with `detail` boolean.
|
||||
* Refactored the router's dynamic route generation.
|
||||
* `list_route` and `detail_route` maintain the old behavior of `url_name`,
|
||||
basing it on the `url_path` instead of the function name.
|
||||
|
||||
* Fix formatting of the 3.7.4 release note [#5704][gh5704]
|
||||
* Docs: Update DRF Writable Nested Serializers references [#5711][gh5711]
|
||||
|
|
|
@ -147,8 +147,8 @@ def action(methods=None, detail=None, url_path=None, url_name=None, **kwargs):
|
|||
def decorator(func):
|
||||
func.bind_to_methods = methods
|
||||
func.detail = detail
|
||||
func.url_path = url_path or func.__name__
|
||||
func.url_name = url_name or func.__name__.replace('_', '-')
|
||||
func.url_path = url_path if url_path else func.__name__
|
||||
func.url_name = url_name if url_name else func.__name__.replace('_', '-')
|
||||
func.kwargs = kwargs
|
||||
return func
|
||||
return decorator
|
||||
|
@ -163,7 +163,13 @@ def detail_route(methods=None, **kwargs):
|
|||
"`action`, which accepts a `detail` bool. Use `@action(detail=True)` instead.",
|
||||
PendingDeprecationWarning, stacklevel=2
|
||||
)
|
||||
return action(methods, detail=True, **kwargs)
|
||||
|
||||
def decorator(func):
|
||||
func = action(methods, detail=True, **kwargs)(func)
|
||||
if 'url_name' not in kwargs:
|
||||
func.url_name = func.url_path.replace('_', '-')
|
||||
return func
|
||||
return decorator
|
||||
|
||||
|
||||
def list_route(methods=None, **kwargs):
|
||||
|
@ -175,4 +181,10 @@ def list_route(methods=None, **kwargs):
|
|||
"`action`, which accepts a `detail` bool. Use `@action(detail=False)` instead.",
|
||||
PendingDeprecationWarning, stacklevel=2
|
||||
)
|
||||
return action(methods, detail=False, **kwargs)
|
||||
|
||||
def decorator(func):
|
||||
func = action(methods, detail=False, **kwargs)(func)
|
||||
if 'url_name' not in kwargs:
|
||||
func.url_name = func.url_path.replace('_', '-')
|
||||
return func
|
||||
return decorator
|
||||
|
|
|
@ -215,3 +215,13 @@ class ActionDecoratorTestCase(TestCase):
|
|||
"3.10 in favor of `action`, which accepts a `detail` bool. Use "
|
||||
"`@action(detail=False)` instead."
|
||||
)
|
||||
|
||||
def test_route_url_name_from_path(self):
|
||||
# pre-3.8 behavior was to base the `url_name` off of the `url_path`
|
||||
with pytest.warns(PendingDeprecationWarning):
|
||||
@list_route(url_path='foo_bar')
|
||||
def view(request):
|
||||
pass
|
||||
|
||||
assert view.url_path == 'foo_bar'
|
||||
assert view.url_name == 'foo-bar'
|
||||
|
|
Loading…
Reference in New Issue
Block a user