2 回答
TA贡献1804条经验 获得超3个赞
实际上,这两种权限都可以合并为一个。例如像这样更新权限:
class CanDeleteUser(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
if request.method in permissions.SAFE_METHODS:
return False
return obj.user == request.user or ob.post.administrator.filter(pk=request.user.pk).exists()
在这里,我正在检查request.useris或检查附加变量obj.user的对象的管理员。showcaseobj
现在我只检查collaborator.
class CollaboratorDeleteView(APIView):
'''
Allow Administrators to delete a collaborator to a showcase
or allow the collaborator user to be able to delete himself
'''
permission_classes = [CanDeleteUser]
def delete(self, request, pk):
collaborator = get_object_or_404(Collaborator, pk=pk)
try:
self.check_object_permissions(request, collaborator)
TA贡献1818条经验 获得超7个赞
您可以permission_classses使用 & (and)、| 添加任意数量的权限来赋予属性 (or) 和 ~ (not) 符号 ( doc ):
class CollaboratorDeleteView(APIView):
'''
Allow Administrators to delete a collaborator to a showcase
or allow the collaborator user to be able to delete himself
'''
permission_classes = [IsAdmin|IsUser]
这两个权限现在都可以使用OR逻辑。
添加回答
举报