DRF权限

权限组件

一、权限介绍

只有具有权限的用户才能指定数据,普通用户不能访问

二、局部使用

  • 自定义一个类,继承BasePermission
class Mypermision(BasePermission):
	message='不是超级用户不能看'
    def has_permission(self, request, view):
        if request.user.user_type == 1:
            return True
        else:
            return False
  • 视图层函数中
class Books(APIView):
    authentication_classes = [MyAuth,]
    permission_classes = [Mypermision,]
    def get(self,request):
        return Response('返回了所有书籍')

三、全局使用:配置文件

REST_FRAMEWORK={
    "DEFAULT_AUTHENTICATION_CLASSES":["app01.service.auth.Authentication",],
    "DEFAULT_PERMISSION_CLASSES":["app01.service.permissions.SVIPPermission",]
}
原文地址:https://www.cnblogs.com/king-home/p/11129942.html