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: relation between permissions and group

Why this works:

g = Group.objects.get(pk=1)
p = g.permissions.first()

Out[43]: <Permission: onboarding | controller | Can add controller>

But this doesn’t work:

p.group
AttributeError: 'Permission' object has no attribute 'group'

When I do:

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

p._meta.__dict__

I see:

'fields_map': {'Group_permissions+': <ManyToOneRel: auth.group_permissions>,
  'group': <ManyToManyRel: auth.group>,
  'User_user_permissions+': <ManyToOneRel: core.user_user_permissions>,
  'user': <ManyToManyRel: core.user>}

So my question is, why can’t I do p.group ?

>Solution :

There is a ManyToManyField relation [Django-doc] between Group and Permission. You can obtain the related Groups for a given Permission with:

p.group_set.all()
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