Sebastian Wagner
Sebastian Wagner

Reputation: 2526

Django Rest Framework: @detail_route and @list_route with same name

I have a DRF ViewSet and would like to provide a detail and list routes with the same name:

class MyViewSet(ViewSet):
    # ...

    @detail_route(['post'])
    def do_stuff(self, request, *args, **kwargs):
        pass

    @list_route(['post'])
    def do_stuff(self, request, *args, **kwargs):
        pass

This obviously doesn't work, as the list_route will just overwrite the detail_route.

Is there a simple way to provide both routes without manually messing up with the urls?

Upvotes: 2

Views: 3022

Answers (1)

Jose Luis Barrera
Jose Luis Barrera

Reputation: 361

I'm not sure if this's what are you looking for, you can set the same url path name for both:

class MyViewSet(ViewSet):
    # ...

    @detail_route(['post'], url_path='do_stuff')
    def do_stuff_detail(self, request, *args, **kwargs):
        pass

    @list_route(['post'], url_path='do_stuff')
    def do_stuff_list(self, request, *args, **kwargs):
        pass

In the first one, the url will be ../"id"/do_stuff and, in the second one, ../do_stuff

Upvotes: 7

Related Questions