-
-
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
Allow multiple @detail_route and/or @list_route for the same URL #2820
Conversation
…different HTTP methods.
I'm not sure how much value in general this would bring into core at the moment. In this case I think this would do better as custom router with this functionality released as a third party package. |
Our routing here currently follows Django's style. |
Tom, José, thank you for your replies. Sorry, but I do not understand what Django's style Tom is referring to here. Please correct me if I'm wrong, but Django itself does not have automatic routing. Or does it? And by adding the possibility to have ViewSet method per HTTP method per ad-hoc URL path, this PR was intended to make handling of ad-hoc routes in ViewSet more consistent with standard ViewSet methods (and consistency is a good thing, isn't it?) Which, in turn, allows to make client code using DRF a bit cleaner (more code paths separation, less indent required), which is also a good thing. Anyways, I estimated the possibility to make this an independent out-of-core router. And while making it in-core required a fairly small amount of code changes, making it outside of the core will required a significant amount of code duplication, which I'd like to avoid, if at all possible. To make implementing this kind of change in an out-of-core package easier, I'd like to suggest the following change to the DRF core. There's a Please let me know if this kind of change is acceptable. I'll submit another PR if it is. Thank you in advance, |
I am 👍 on this PR. There are additional benefits of splitting out the method such as having different permission classes on each method (see #2708). |
Any way we can re open this PR for discussion? I think that having separate python methods would also help tools like |
@tomchristie Any possibility of reopening this for discussion? |
Just to put it out there, I am the third 👎 on this PR because I don't see this as being something that needs to be in the core, and I see it introducing more confusion on top of the already confusing
Django enforces the idea of having a single view for a url route. This is something that I agree with Django REST framework maintaining, as another who is used to Django views can safely assume that a route will have a single view.
This is something that I wouldn't necessarily be opposed to, but I wouldn't mind hearing some input from @tomchristie or @jpadilla about it. The alternative implementation that I was going to suggest for setting up detail routes with different method calls was class UserViewSet(ModelViewSet):
...
@detail_route(methods=['post', 'delete'])
def password(self, request, pk=None):
if request.method == 'POST':
return self.ret_password(request, pk=pk)
else:
return self.reset_password(request, pk=pk)
def set_password(self, request, pk=None):
# set password
...
def reset_password(self, request, pk=None):
# reset (delete) password
... At least this will make it more obvious what the |
While changing The nice thing about breaking up the separate actions as separate methods is that there is now space to attach separate annotations to the methods. This is important to accomplish #2062 and for tools like |
I'm also -0 for this change. |
At this momento without this functionality I have to choose between a working function or a documented function with django rest swagger 2. The new schema doesn't render more than one verb per function in detail and list routes. |
Currently, if I need an ad hoc route to handle several HTTP methods I'm expected to define a single method in a view set, and then explicitly check for the value of the
request.method
to decide what code path to choose.Example. Lets assume I have a
UserViewSet
similar to the one from the DRF's documentation. I'd like a single sub-URL to be used for both setting and resetting (deleting) the user's password. Currently, I can do this like follows:This Pull Request allows to define several decorated viewset methods with the same
url_path
but differentmethods
, and lets DRF route requests to these methods automatically based on both URL the HTTP method, just like with predefinedlist
,get
,update
etc. viewset methods. In the client side code this will look like follows: