-
-
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
Mark extra actions which corresponds to the same URL #4920
Comments
That's an interesting idea. |
Is there a reason to have the two separate url patterns? You could also simulate this with the following: class MyViewSet(viewsets.ViewSet):
@detail_route(methods=['get', 'head'], url_path='foo', url_name='foo')
def foo(self, request, pk=None):
if request.method == 'get':
return self.foo_get(request, pk)
if request.method == 'head':
return self.foo_head(request, pk)
def foo_get(self, request, pk):
pass
def foo_head(self, request, pk):
pass
|
As per @rpkilby. |
@tomchristie @rpkilby It is not what I expect, because in the above code there is one action named # The value of self.action is `foo`.
action = self.action What I desire is to have two actions corresponding to the same url, and the action is changed based on the HTTP method (like get -> list and post -> create). It is something that is not supported by the |
Actually that's kinda fair, tho it's not obv. if we'd do better to just accept this as one of the limitations of viewsets or cater for this case better. It'd get slightly complicated in that you'd also still be able to have multiple methods for an particular action. Would certainly be will to review a pull request for this on it's own merits. |
One would argue that's already the case with list / create and retrieve / update / delete. |
Reopening for further consideration. |
This is the same as #2820, #3743, and #4264. I will note that either way the |
In my opinion, #2820 is on the right direction. For the reasons I explained above, I believe that this feature is not trivial, and indexing by url patterns makes code consistent. |
Just a quick note; The mini If we want different documentation from the Currently it does not handle schema documentation as described here: http://www.django-rest-framework.org/api-guide/schemas/#schemas-as-documentation |
Hi all. I've implemented this in #5605. Current usage looks like: class MyViewSet(ViewSet):
@action(detail=False)
def example(self, request, **kwargs):
"""Base description."""
@example.mapping.post
def create_example(self, request, **kwargs):
"""Description for creation.""" Per @kevin-brown's comment:
Method mapping intentionally does not accept any arguments, so you would be unable to run into this. Lastly, method mapping works as expected with schemas/documentation. |
Imagine that I have two extra actions that corresponds to the same url (e.g. like
create()
withlist()
which corresponds to the name url i.e.^{prefix}/{name}/
but with different HTTP methods).I want to use the
DefaultRouter
class to construct url patterns rather than creating urls on my own like follows:The problem here is that the
_get_dynamic_routes()
method ofSimpleRouter
class create a namedtuple (Route
) for every extra action, and therefore only the first extra action is taken into account. So, the fist call succeeds whereas the second call returns HTTP 405 METHOD_NOT_ALLOWED.All in all, I would expect that one
Route
should be created by the_get_dynamic_routes()
with the following mapping (Remeber that I want these extra actions to correspond to the same url!):The text was updated successfully, but these errors were encountered: