如何在Django Rest Framework中的视图集中为自定义动作添加特定级别的访问权限?
如何在Django Rest Framework中的视图集中为自定义动作添加特定级别的访问权限?
我在Django Rest Framework中拥有一个ModelViewSet,具有所有的增删改查功能,并将权限设置为IsAuthenticated,但我创建了一个自定义的action,并希望它是公开的,但我不知道如何做到这一点,因为文档只展示了如何使用@api_view()来实现这一点,这是我的ModelViewSet:
class UserViewSet(viewsets.ModelViewSet): """User viewset""" queryset = User.objects.all() serializer_class = UserModelSerializer authentication_classes = [TokenAuthentication] permission_classes = [IsAuthenticated] @action(detail=False, methods=['post']) def signup(self, request): """Sign Up users without profile. """ serializer = UserSignUpSerializer(data=request.data) if serializer.is_valid(raise_exception=True): user = serializer.save() data = { 'user': user, } return Response(data, status=status.HTTP_201_CREATED) @action(detail=False, methods=['post']) def login(self, request, *args, **kwargs): """ Handle logins request. """ serializer = UserLoginSerializer(data=request.data) serializer.is_valid(raise_exception=True) user, token = serializer.save() data= { 'user': user, 'access_token': token } return Response(data, status=status.HTTP_201_CREATED)
我想让login这个@action是公开的,希望你们能帮助我。