用户组和权限 [英] User groups and permissions

查看:152
本文介绍了用户组和权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为用户组实现用户权限(与Facebook组非常相似)。例如,每个组可以拥有如下权限的成员:can_post,can_delete,can_ban等。当然,一个用户可以是许多组的成员,组可以具有不同权限的许多不同的用户。
这个功能需要什么样的模型?

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, etc. Of course, one user can be a member of many groups and group can have many different users with different rights. What models i need for this functionality?

推荐答案

Django有一个内置的组系统。每当你有这样的问题,我建议搜索Django文档 ,这是广泛,有帮助和写得很好。

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.

只要你使用 django.contrib.auth 应用程序,您可以访问组。然后,您可以为这些组分配权限。

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)

这篇关于用户组和权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆