Permissions components

Rights 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

 

Usage rights component

In the Permissions class:

from rest_framework.permissions Import the BasePermission   # Import module inherits 

class MyPermision (the BasePermission): 
    Message = ' not a super user to view not '   # If no Message, returns the front-end English 
    DEF has_permission (Self, Request, View):
         IF == 1 request.user.user_type :
             return True
         the else :
             return False

In the view class:

class Books (APIView):
     # authentication_classes = [MyAuth,] # here commented out because setting global use in settings where 
    # only super users can access this interface 
    permission_classes = [MyPermision,]
     DEF GET (Self, Request):
         # Request .user is currently logged on user, in order to get to the front must return 
        Print (request.user.name)
         return the Response ( ' return all books ' )

Display effects:

Ordinary users access after logging in, because it is not super user can not access.

But the error is in English, how to change the Chinese, it is necessary to specify message = 'Chinese'

 

Superuser logged in to view all books interfaces

 

1, topical

2, the global use

REST_FRAMEWORK={
    "DEFAULT_AUTHENTICATION_CLASSES":["app01.MyAuths.MyAuth",],
    "DEFAULT_PERMISSION_CLASSES":["app01.MyAuths.MyPermision",]
}

3、局部禁用

permission_classes = []

 

Guess you like

Origin www.cnblogs.com/zhangguosheng1121/p/11129916.html