views.py
from rest_framework.response import Response
from rest_framework.views import APIView
#局部自定义认证逻辑
class MyAutheentication(object):
def authenticate(self,requeest):
#这里加需要认证的逻辑,获取token查数据库啥的,这里return的下面用request.use去拿
return ("yuan",None)
class BookView2(ModelViewSet,APIView):
# 局部自定义认证使用,有的话就不会去拿setting文件里面的DEFAULT_AUTHENTICATION_CLASSESS
authentication_classes = [MyAutheentication,]
def zz(self,request):
#获取用户类型
print("request.user",request.user)
return Response(123)
settings.py
# 全局认证 ,如果views.py里面没有单独写,那么就默认取这边的
REST_FRAMEWORK={
"DEFAULT_AUTHENTICATION_CLASSESS":[]
}
标签:return,自定义,views,py,request,认证,组件,drf From: https://www.cnblogs.com/kaibindirver/p/16845052.html