djangodjango-guardian

Django guardian several permissions with get_objects_for_user


If I pass in one permission at a time get_objects_for_user works fine

>>> 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.>]>

Now from the docs

It is also possible to provide list of permissions rather than single string,

But this does fail to return anything

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

what am I doing wrong when passing the permissions list?


Solution

  • I gues you need to add any_perm=True

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

    Docs

    ā€¢ any_perm ā€“ if True, any of permission in sequence is accepted. Default is False.