我明白,我们可以建立认证类基于类viewsets这样的:动态地改变身份验证类Django的REST框架
class ExampleViewSet(ModelViewSet):
authentication_classes = (SessionAuthentication, BasicAuthentication)
然而,有没有办法来动态改变基于请求认证类方法?我想在我的ExampleViewSet
重写此功能:
def get_authenticators(self): # Found in
if self.request.method == "POST":
authentication_classes.append(authentication.MyCustomAuthentication)
return authentication_classes
然而,Django的休息没有request
对象设置在这一点上:
'ExampleViewSet' object has no attribute 'request'
注:不是真正的变量名 - 只是举例的目的。
这不起作用,但给出了一种新的错误类型。 – lbrindze
@lbrindze那是什么错误? –
使用django 1.11与python3它说get_authentictors期待2个参数,但只收到一个(这是有道理的,因为这个重写指定自我和请求作为位置参数)。 我认为我的用例https://stackoverflow.com/questions/19773869/django-rest-framework-separate-permissions-per-methods似乎让我想要什么。 – lbrindze