how to create a group permission in django

前端 未结 2 1628
鱼传尺愫
鱼传尺愫 2020-12-30 09:01

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         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 09:39

    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.

提交回复
热议问题