ybendana
ybendana

Reputation: 1643

Django Rest Framework @list_route requires pk

I'm using DRF 2.4.4 and running into this issue where the @list_route decorator requires a pk. Here is my code excerpt:

class RunViewSet(ModelViewSet):
    serializer_class = RunSerializer
    queryset = Run.objects.all()

    @list_route()
    def active(self, request, pk):
        '''Return active runs.'''
        qs = Run.objects.all(deleted=False)
        serializer = RunSerializer(qs, many=True)
        return Response(serializer.data)

If I try to access the endpoint at /api/runs/active I get a 404 error. It only works if I give a pk such as /api/runs/1/active. Since @list_route is supposed to operate on a collection, why do I need to give it a pk?

Upvotes: 2

Views: 3543

Answers (2)

Moamen
Moamen

Reputation: 706

If anyone still have this problem, you can use solution suggested here.

If you want to use nesting facilities you should mixin NestedRouterMixin into your router.

What I did:

from rest_framework.routers import DefaultRouter
from rest_framework_extensions.routers import NestedRouterMixin


class NestedDefaultRouter(NestedRouterMixin, DefaultRouter):
    pass

# Then you can use your router as usual
router = NestedDefaultRouter()

Upvotes: 4

ybendana
ybendana

Reputation: 1643

This turned out to be a problem with the Extended Routers of DRF extensions.

Upvotes: 4

Related Questions