Using a permission class on a detail route

前端 未结 3 1577
执笔经年
执笔经年 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:07

    Update-1

    From DRF 3.8 onwards, detail_route decorator has replaced with action decorator.

    class EventViewSet(viewsets.ModelViewSet):
        @action(permission_classes=[permissions.PermissionClass_], methods=['post'])
        def messages(self, request, pk=None):
            # your view code
    

    Original post

    You can add permissions basically by doing this:

    class EventViewSet(viewsets.ModelViewSet):
        @detail_route(
            permission_classes=[
                permissions.PermissionClass_],
            methods=['post'])
        def messages(self, request, pk=None):
            ### Check a permissions class.
            ...
    

提交回复
热议问题