Search code examples
django-rest-frameworkdjango-rest-viewsets

Call view from another view


I have a viewset with one of the views as:

@list_route(methods=["get"], url_path="special")
def special():
    pass

And I call this view from another view like:

view_fn = viewset.as_view({'get': 'list'})
response = view_fn(request)

But it does not call my special function which maps to "/special/", instead it calls the function which maps to "/". I guess I need to pass url_path somehow or get the view using view name? However, I'm not sure how to do either.


Solution

  • This will not work because you need to map that route with the action.

    In its current form, you are mapping the default list action to the get method.

    The following code should work:

    view_fn = viewset.as_view({'get': 'special'})