Imagine that I have two extra actions that corresponds to the same url (e.g. like create() with list() which corresponds to the name url i.e. ^{prefix}/{name}/ but with different HTTP methods).
from rest_framework import viewsets
from rest_framework.decorators import detail_route
class MyViewSet(viewsets.ViewSet):
@detail_route(methods=['get'], url_path='foo', url_name='foo_get')
def foo_get(self, request, pk=None):
pass
@detail_route(methods=['head'], url_path='foo', url_name='foo_head')
def foo_head(self, request, pk=None):
pass
I want to use the DefaultRouter class to construct url patterns rather than creating urls on my own like follows:
extra_actions = MyView.as_view({'head': 'foo_head', 'get': 'foo_get'})
The problem here is that the _get_dynamic_routes() method of SimpleRouter 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.
# This call woks.
curl -X get http:localhost:8000/api/products/1/foo/
# This returns 405.
curl -X head http:localhost:8000/api/products/1/foo/
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!):
mapping = {
'get': 'foo_get',
'head': 'foo_head',
}
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
DefaultRouterclass to construct url patterns rather than creating urls on my own like follows:The problem here is that the
_get_dynamic_routes()method ofSimpleRouterclass 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
Routeshould be created by the_get_dynamic_routes()with the following mapping (Remeber that I want these extra actions to correspond to the same url!):