Using a permission class on a detail route

前端 未结 3 1576
执笔经年
执笔经年 2021-02-13 16:24

How can I only apply a permission class to a detail route?

class EventViewSet(viewsets.ModelViewSet):

    @detail_route(methods=[\'post\'])
    def messages(sel         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 17:08

    in django rest 3.8 detail route is deprecated in favor of action route so now it would look something like this.

    from rest_framework.decorators import api_view, permission_classes
    
    class EventViewSet(viewsets.ModelViewSet):
    
        @action(
            methods=['post'],
            detail=True,
            permission_classes=[YourPermission]
            url_path='messages')
        def messages(self, request, pk=None):
            return Response("200", status=200)
    

提交回复
热议问题