Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading