Question

I'm trying to create an API end point that returns the current Screener. A Screener is a one-to-one model with a user. I would like to hit this end point and have all the necessary info returned, however, I would like it to return with out passing any params. Right now when using the @link() decorator the url becomes screener/{look-up-field}/current. I would like it not have the look up field in the url. My viewset:

class ScreenerViewSet(viewsets.ModelViewSet):
    model = Screener
    serializer_class = ScreenerSerializer
    permission_classes = (ObjectPermissions,)
    queryset = Screener.objects.all()

    @link()
    def current(self, request, *args, **kwargs):
        screener = get_object_or_404(Screener, user__id=request.user.id)
        data = self.serializer_class(screener)
        return Response(data=data.data, status=status.HTTP_200_OK)

My routes:

router = DefaultRouter()
router.register(r'api/screener', ScreenerViewSet)
urlpatterns += router.urls
Was it helpful?

Solution

the default DRF @link decorator only works on the detail-level.

For collection-level @link and @action decorators you can use drf-extensions, which provides these.

OTHER TIPS

Can you remove *args and **kwargs from the method definition? I this is the solution but right now I can not test it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top