User groups and permissions

后端 未结 1 1467
清歌不尽
清歌不尽 2020-11-29 02:19

I need to implement user rights for user groups (pretty similar to facebook groups). For example, each group can have members with rights like: can_post, can_delete, can_ban

1条回答
  •  情深已故
    2020-11-29 03:03

    Django has a built in groups system. Whenever you have a question like this, I recommend searching the Django docs, which are extensive, helpful, and well written.

    So long as you are using the django.contrib.auth app, you have access to groups. You can then assign permissions to those 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)
    

    0 讨论(0)
提交回复
热议问题