how to create a group permission in django

Atma picture Atma · Sep 14, 2013 · Viewed 26.3k times · Source

I am attempting to create a row in the auth_group_permission table.

I have tried the following:

group_permission = group_permissions.add(group=group, permission=permission)

group_permission = group.group_permissions_set.add(permission=permission)

group_permission = group.permissions_set.add(permission=permission)

None of these work. Does anyone know how to add a record to this table?

Answer

Trewq picture Trewq · Sep 14, 2013

The following answer helped me in setting up groups.

from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get(app_label='myapp', model='BlogPost')
permission = Permission.objects.create(codename='can_publish',
                                       name='Can Publish Posts',
                                       content_type=content_type)
user = User.objects.get(username='duke_nukem')
group = Group.objects.get(name='wizard')
group.permissions.add(permission)
user.groups.add(group)

You can add this permissions through the shell python manage.py shell and then entering the code above.