Django guardian几个权限get_objects_for_user

v1uwarro  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(108)

如果我一次传入一个权限,get_objects_for_user工作正常

>>> projects = get_objects_for_user(alvin, 'view_project', klass=Project)
>>> projects
<QuerySet [<Project: Central whole.>]>
>>> projects = get_objects_for_user(alvin, 'change_project', klass=Project)
>>> projects
<QuerySet [<Project: Education soldier.>, <Project: Evening cold.>]>

现在从文档来看
也可以提供许可列表而不是单个字符串,
但这并不能返回任何东西

>>> projects = get_objects_for_user(alvin, ('change_project', 'view_project'), klass=Project)
>>> projects
<QuerySet []>

我在传递权限列表时做错了什么?

rhfm7lfc

rhfm7lfc1#

我猜你需要添加any_perm=True

projects = get_objects_for_user(
    alvin, 
    ('change_project', 'view_project'), 
    klass=Project, 
    any_perm=True,
)

文档
· any_perm -如果为True,则接受顺序中的任何权限。默认值为False。

相关问题