Permissions Introduction
Only the super-user can access the specified data, the average user can not access, so we'll have to limit the authority component
Source section
Permissions categories:
class MyPermission(): def has_permission(self,request,view): if request.user.user_type == 1: return True else: return False
View class:
class Books (APIView): # authentication_classes = [class name, class name] # Only superusers can access this interface permission_classes = [MyPermission,] DEF GET (Self, Request): Print (request.user.name) return the Response ( ' return all books ' )
If you are a regular user login is a return error messages in English, he should be converted into Chinese
As long as the method above price message
Need to inherit
from rest_framework.permissions import BasePermission
局部使用
permission_classes = [MyPermission,]
全局使用
因为是个列表,直接后面加上
REST_FRAMEWORK={ "DEFAULT_AUTHENTICATION_CLASSES":["app01.MyAuths.MyAuth",], "DEFAULT_PERMISSION_CLASSES":["app01.MyAuths.MyPermision",] }
局部禁用
permission_classes = []