-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to customize router URLs for custom actions, using url_path
.
#2010
Conversation
I can't see changing the methods being a standard, typical use-case. |
Cool.. the standard use case for me here was to convert _ to - (underscores to hyphen). For ex: I had a method sync_id for which I wanted to have a URI like prefix/sync-id. |
agree with @tanwanirahul, a bit annoying have to install |
Someone probably needs to walk through a simple example on this? |
I am not sure what you mean. |
Okay, understood. |
@maryokhin Yes, agree. Its a pain to use extensions for a very trivial tasks like this one. |
Not going to promise that it's high on the list right now given getting 3.0 out the door, but certainly seems worth looking at at least. |
Cool..makes complete sense. |
Hi @tomchristie, Are there any thoughts on this as of today? The reason I'm asking is because of the suspended state of the whole dynamic route thing. There are reports of routes not working properly in So it's unclear whether the effort should be spent on making |
Confused by the question.
So are there any other examples other than selecting between
I don't know what this means? What dynamic routes? |
I meant the
Not really, which is why I proposed in #1892 to just add
I meant the routes that are created when decorating a viewset method with |
In which case I'd prefer to see a
Notice It's not terribly elegant, but it only needs to be done once. I'd be against any further complexity in the router classes as they're already the least nice bit of API in rest framework so I've no problem with them continuing to only provide for a fairly restricted set of functionality. |
It probably doesn't matter, but I was talking about urls, not base names. Being able to use: UserViewSet(ModelViewSet):
...
@detail_route()
def request_password_reset(...):
.... And get a url Which is why I felt that it belongs with the |
The bottom line with third party packages is that they're not significantly any more work to implement (given @jpadilla's excellent work on the cookiecutter package) but are simply a good way to distribute maintainance responsibilities. I don't have any interest in expanding the API set of the built-in routers - I know from experience that it'll only mean extra ongoing support cost to the project, which then negatively impacts our ability to build other new functionality or keep on to of bug reports. By all means let's do this, but let someone else take it on in a separately installable package. |
@tomchristie Ability to change the url pattern (to make it different from method_name) would be as useful as ability to change the table names in django models. Sure we can do this with:
I wont be surprised if this request come up again. |
Your foreshadowing is appropriate, @tanwanirahul. I'm bringing this up again. 😄 I'm new to DRF and I'm really surprised this use case isn't supported, and I'm a little more surprised that there's so much resistance to it. I have a specific case where the limitations are causing me some frustration. Example: router = routers.DefaultRouter()
router.register('things', ThingViewSet)
class ThingViewSet(ViewSet):
@detail_route(methods=['GET'])
def get_thing_widgets(self, request, pk=None):
"""
I want this to handle /things/{pk}/widgets
Instead it handles /things/{pk}/get-thing-widgets
"""
@list_route()
def get_all_widgets(self, request):
"""
I want this to handle /things/widgets
Instead it handles /things/get-all-widgets
""" As far as I can tell, what I want isn't actually possible with DRF without going to great lengths with extensions and/or customized routers, which I think is a little silly because it's a fairly trivial case. What I really want is the ability to name my methods in such a way that they convey their purpose when looking at the code and have the URL be completely independent. @tanwanirahul's patch takes care of this with very little effort expended. |
Okay, reopened. PR needs docs. |
@tomchristie Hmmm, I would take that on me. Will add by EOD today. |
If we accept this format, then we should also think about the naming of the decorator |
👍 |
url_path
.
Creating a custom router just to change the method name in the URI is too verbose, specially if you have many custom actions in a ViewSet and you only to customize single method.
This patch would allow developers to customize the default method name with something else by passing a custom_method_name as a part of list_route and detail_route decorators.