可以通过重写get_permissions方法和get_serializers_class方法来实现动态的配置权限和序列化

 

VIewDemo

class RegUserSet(mixins.CreateModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    serializer_class = RegUserSerializer
    # permission_classes = ()
    # queryset = User.objects.all()

    def get_permissions(self):
        if self.action == 'retrieve':
            return [IsAuthenticated()]
        else:
            return []

    def get_serializer_class(self):
        pass

    def get_object(self):
        return self.request.user

 

posted on 2019-04-06 18:01  chester·chen  阅读(745)  评论(0编辑  收藏  举报