Suppose I have a view as
from rest_framework import viewsets
class SampleViewset(viewsets.ModelViewSet):
serializer_class = SampleSerializer
queryset = Sample.objects.all()
authentication_classes = (FirstAuthClass, SecondAuthClass, ThirdAuthClass)
def get_success_auth_class(self, request, *args, **kwargs):
# What logic should I use here ????
return success_auth_class
Then, What logic should I use in get_success_auth_class()
method?
In other words, How can I get the Authentication class (or class name) which is succeeded during authentication in DRF View?
The DRF Request class has a property called successful_authenticator
which will return the instance of the authentication instance class that was used to authenticate the request, or None
.
class SampleViewset(viewsets.ModelViewSet):
serializer_class = SampleSerializer
queryset = Sample.objects.all()
def get_success_auth_class(self, request, *args, **kwargs):
for auth_class in self.authentication_classes:
if isinstance(, auth_class):
return auth_class
return None
Quoting the source code here
@property def successful_authenticator(self): """ Return the instance of the authentication instance class that was used to authenticate the request, or `None`. """ if not hasattr(self, '_authenticator'): with wrap_attributeerrors(): self._authenticate() return self._authenticator
Reference :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.