Hippolyte BRINGER
Hippolyte BRINGER

Reputation: 853

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:

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 ?

Upvotes: 1

Views: 101

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

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()

Upvotes: 1

Related Questions