Django rest framework 当不使用操作时,如何使用身份验证装饰器来解决身份验证问题

Django rest framework 当不使用操作时,如何使用身份验证装饰器来解决身份验证问题,django-rest-framework,Django Rest Framework,我尝试使用身份验证装饰器,但它不起作用,下面是我的代码 class UserViewSet(CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericViewSet): print('enter') queryset = UserProfile.objects.all() @authentication_classes([JwtAuthorizationAuthent

我尝试使用身份验证装饰器,但它不起作用,下面是我的代码

class UserViewSet(CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericViewSet):
    print('enter')
    queryset = UserProfile.objects.all()
    @authentication_classes([JwtAuthorizationAuthentication,])
    def create(self, request, *args, **kwargs):
        print('creat')
        print(request.query_params)
        return ('ok')
    
    def retrieve(self, request, *args, **kwargs):
        print(f'1{self.authentication_classes}')
        print('ok2')
        return Response('ok')
我只想在使用创建和更新操作时使用JWTAuthentication,而不想对视图集中的其他操作进行更多的验证

我也尝试编写装饰程序来解决这个问题,但是auth_装饰程序不起作用

from functools import update_wrapper

def auth_wrapper(*authentications, validate_auth=True):
    def decorator(func):
        def wrapper(self, request, *args, **kwargs):
            self.authentication_classes=authentications
            print(self.authentication_classes)
            if validate_auth:
                print(f'request1:{request.user}')
                self.perform_authentication(request)
                print(f'request2:{request._user}')
                # print(type(self.perform_authentication(request)))
     
            return func(self, request, *args, **kwargs)
        return update_wrapper(wrapper, func)
    return decorator

def permission_wrapper(*permissions, validate_perm=True):
    def decorator(func):
        def wapper(self, request, *args, **kwargs):
            self.permission_classes=permissions
            if validate_perm:
                self.check_permissions(request)
            return func(self, request, *args, **kwargs)
        return update_wrapper(wapper, func)
    return decorator




有人能帮我解决这个问题吗?多谢各位

def get_authenticators(self):
    if is_post_method(post):
        return [JwtAuthorizationAuthentication,]
    return super().get_authenticators()
我用上面的代码解决了这个问题