我试图使用Django Guardian授予许可。当我尝试授予现有数据的权限时,它会向我显示一个错误的消息,但是当我创建一个新对象时,它的表现为真。我做错了什么?
我的代码:
>>>from django.contrib.auth.models import User
>>>from print.models import *
>>>from guardian.shortcuts import assign_perm
>>>user = User.objects.create(username='tanvir',password='antu')
>>>excel = ExcelData.objects.all()
>>>assign_perm('delete_exceldata', user, excel)
>>>user.has_perm('delete_exceldata', excel)
>>>False
,但是如果我做
>>>from django.contrib.auth.models import User
>>>from print.models import *
>>>from guardian.shortcuts import assign_perm
>>>user = User.objects.create(username='tanvir',password='antu')
>>>excel = ExcelData.objects.create(order_number='01245632145214')
>>>assign_perm('delete_exceldata', user, excel)
>>>user.has_perm('delete_exceldata', excel)
>>>True
excel = ExcelData.objects.all()
会给您一个QuerySet和
excel=ExcelData.objects.create(order_number='1245632145214')
会给你一个对象..
您只能将权限分配给对象
如果要分配QuerySet的权限,请在循环中
user = User.objects.create(username='tanvir',password='antu')
excel = ExcelData.objects.all()
for obj in excel:
assign_perm('delete_exceldata', user, obj)
user.has_perm('delete_exceldata', obj) # this will give you status for each obj