Home > database >  Django: relation between permissions and group
Django: relation between permissions and group

Time:04-13

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 ?

CodePudding user response:

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