restful access control
# 写一个权限类,继承BasePermission
class MyPremission(BasePermission):
# 重写has_premission方法
message = "中文错误信息" # 读源码可以知道 返回值显示中文,写message ="中文"
def has_premission(self,request,view):
# 下面写权限控制相关逻辑
# 因为权限校验在认证之后,所以能取到request.user
if request.user。user_type == 1:
return True # 查看源码发现 返回值是bool值
else:
return False
Usage rights component
Partial view in
Write in the view class
permission_classes=[MyPermision,]
Global Use
The need to configure setting.py
REST_FRAMEWORK={ "DEFAULT_PERMISSION_CLASSES":["app01.MyAuths.MyPermision",] }
After the global settings, disable the local
# 在视图类中写 permission_classes = []