基本上,您会得到类似的东西: class IsAuthenticatedOrCreate(permissions.IsAuthenticated):def has_permission(自己,请求,查看):如果request.method =='POST':返回True返回super(IsAuthenticatedOrCreate,self).has_permission(请求,查看) 其他权限类也可能与此类似.I have the following ModelViewSetclass UserViewSet(viewsets.ModelViewSet): """ API endpoint that allows users to be viewed or edited. """ queryset = User.objects.all() serializer_class = UserSerializer authentication_classes = (TokenAuthentication,) permission_classes = (permissions.IsAuthenticated, MyUserPermissions)I want the create method (POST on /users/) to not ask for any authentication. How can I override the authentication_classes in this case? I'm talking about ModelViewSet not generic API views. 解决方案 Actually that's not quite what you want. You want POST on users to not require any permissions, which will have the effect that either authenticated or unauthenticated requests will succeed.I'd suggest overriding your permission classes so that they always allow POST requests. Follow the custom permissions documentation for more info on that.Essentially you'll have something like:class IsAuthenticatedOrCreate(permissions.IsAuthenticated): def has_permission(self, request, view): if request.method == 'POST': return True return super(IsAuthenticatedOrCreate, self).has_permission(request, view)And probably something similar for your other permission class too. 这篇关于在Django REST框架的ModelViewSet中对不同的操作使用不同的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 17:06