问题
我正在使用Django Rest Framework。在那种情况下ViewSet
。我只想使用一个REST操作Token Authentication
。DRF是否提供甚至提供类似的功能?
感谢您的帮助。
class UserProfileViewSet(viewsets.ModelViewSet):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
#Here the Token Authentication should be
def destroy(self, request, pk=None, **kwargs):
try:
user = User.objects.get(pk=pk)
user.delete()
except User.DoesNotExist:
return Response(status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_204_NO_CONTENT)
重写get_authenticators()
方法below.Theget_authenticators()
方法实例化并返回鉴定人的名单,这个视图可以使用。在你的情况下,该方法将返回/验证TokenAuthentication
如果动作破坏,( HTTP DELETE
)
from rest_framework.authentication import TokenAuthentication
class UserProfileViewSet(viewsets.ModelViewSet):
# your code
authentication_classes = (TokenAuthentication,)
def get_authenticators(self):
if self.action == 'destroy':
return super().get_authenticators()
return []
def destroy(self, request, pk=None, **kwargs):
# your code
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句