1

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.