9-2 动态设置serializer和permission获取用户信息
-----------------------------------
from rest_framework import permissions
看源码:
重载:
def get_permissions(self):
if self.action == "retrieve":
return [permissions.IsAuthenticated()]
elif self.action == "create":
return []
return []
进来了
action = 'retrieve'
--------------------------------------
from rest_framework import authentication
authentication_classes = (authentication.SessionAuthentication, )
-------------------------------
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
authentication_classes = (JSONWebTokenAuthentication, authentication.SessionAuthentication)
效果:
现在设置:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}
# REST_FRAMEWORK = {
# 'DEFAULT_AUTHENTICATION_CLASSES': []
# }
说:users下面多了个read 之前是没有的。
read一下
还提示:
{
"detail": "身份认证信息未提供。"
}
下面添加 token
再次获取 read
-----------------------------------------
def get_serializer_class(self):
if self.action == "retrieve":
return UserDetailSerializer
elif self.action == "create":
return UserRegSerializer
return UserDetailSerializer
效果: