@action hyphenates url_name but not url_path #10057
|
just stumbled across this. the more I think about it, the stranger it seems to me. @action(detail=True)
def doing_something(self, request)creates an action with url_name "doing-something" and url_path "doing_something": django-rest-framework/rest_framework/decorators.py Lines 223 to 228 in b578eab It would make sense to me if the anyway, would be nice to have some settings for that |
Replies: 2 comments
|
It's intentional rather than a mix-up. The two attributes serve different consumers, and the docstring right above the lines you linked spells out both defaults:
If you want hyphenated paths across a project today, a thin wrapper does it without touching DRF: from rest_framework.decorators import action as drf_action
def action(*, url_path=None, **kwargs):
def decorator(func):
path = url_path or func.__name__.replace("_", "-")
return drf_action(url_path=path, **kwargs)(func)
return decoratorChecked against DRF 3.18.1 with a
|
I agree, as I prefer hyphens in URL paths, but I would argue that it's a bit subjective.
I think this is to be consistent with how the default
There is already a hook to customise this in user-land: @action(detail=True, url_path="doing-something")
def doing_something(self, request):
...And as outlined above, you may implement a wrapper for making this convention consistent in your own project. At the current stage of DRF's lifecycle, I'd say that it's now an established API to me, that we're unlikely to change. I don't see a lot of value in introducing a setting for it... |
I agree, as I prefer hyphens in URL paths, but I would argue that it's a bit subjective.
I think this is to be consistent with how the default
ViewSetactions are named:basename-detail,basename-list... But, yes, that's a different convention that what Django chose for the contrib admin app, for example. Again it's subjective IMO, different people prefer different approaches here.There is already a hook to customise this in user-land: