How to connect a renderer to a specific endpoint i

2019-06-13 20:09发布

The DRF documentation shows how to connect a renderer to an APIView, but not how to do it for a specific action in a ViewSet. Given:

class XViewSet(ViewSet):
    serializer_class = XSerializer

    @action(detail=True, methods=['get'])
    def my_action(self, request, pk=None):
         ..

How do I set a specific renderer for my_action, that will not affect the other/default actions in the viewset?

I can make an APIView just for that action of course but that makes for a more messy urls.py

1条回答
虎瘦雄心在
2楼-- · 2019-06-13 20:34

As far as I can tell, the action takes any argument that can be a class attribute:

class XViewSet(ViewSet):
    serializer_class = XSerializer

    @action(detail=True, methods=['get'], renderer_classes=[yourrenderer])
    def my_action(self, request, pk=None):
         ..
查看更多
登录 后发表回答