Django - 如何在组上添加多个权限?
我有一个权限列表
list = ['view', 'add', 'edit']
列表中的权限已保存在我的表中。
我首先清除该组以前的权限,以便可以插入新权限
group = Group.objects.get(name='Group1')
group.permissions.clear()
有没有办法以编程方式将权限列表添加到 Group1?
I have a list of permissions
list = ['view', 'add', 'edit']
The permissions inside the list are already saved on my table.
I first clear the group's previous permission so I can insert a new one
group = Group.objects.get(name='Group1')
group.permissions.clear()
Is there a way to add the list of permission to Group1 programmatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您有一个名为
blog
的应用和一个名为BlogPost
的模型,并且想要添加权限view
、change
、< code>add 将该模型添加到名为Blogger
的组中,您应该能够执行以下操作:注意:检查
app_label
可能不是必需的,但是它确保您不会为模型添加权限来自另一个应用程序的BlogPost
(如果存在)。查看数据库中的表
auth_permission
和django_content_type
中的字段和可能的值。Say you have an app named
blog
and a model namedBlogPost
and want to add permissionsview
,change
,add
for that model to the group namedBlogger
, you should be able to do:Note: The check for the
app_label
is probably not necessary, but it ensures that you do not add permissions for a modelBlogPost
from another app, if one exists.Have a look at tables
auth_permission
anddjango_content_type
in your db for the fields and possible values.