mcd
mcd

Reputation: 7136

Django Rest Framework nested APIView route

If I have a ModelViewSet at

/foo

is it possible to use an APIView at a url under that? Example APIView would be at

/foo/count?

I tried registering the url with Django but it didnt work except if I changed the /foo to something else?

Upvotes: 1

Views: 620

Answers (2)

Ivan Semochkin
Ivan Semochkin

Reputation: 8897

You could use @list_route decorator

from rest_framework.decorators import list_route

class FooViewSet(viewsets.ModelViewSet):

    @list_route()
    def count(self, request):
       ...
       return Response(...)

It will append new endpoint to viewset url /foo/count/ like you want
Docs about extra actions

Upvotes: 1

mcd
mcd

Reputation: 7136

It worked if I registered the url with Django before I registered the ModelViewSet url patterns.

urlpatterns += [
url(
    regex=r'^foo/count/',
    view=Foo.as_view(),
    name='foo_count',
    )
]
urlpatterns += foo_urlpatterns

Upvotes: 0

Related Questions