如何在Django中以编程方式创建组和权限?

时间:2016-04-07 13:32:47

标签: django permissions usergroups

我尝试创建群组并为其添加一些权限。 这就是我所做的:

students = Group(name='Students')
students.save()   

somemodel_ct = ContentType.objects.get(app_label='course', model='course')

can_view = Permission(name='Can View', codename='can_view_something',
                       content_type=somemodel_ct)
can_view.save()

can_modify = Permission(name='Can Modify', codename='can_modify_something',
                       content_type=somemodel_ct)

can_create = Permission(name='Can Create', codename='can_create_something',
                       content_type=somemodel_ct)

can_delete = Permission(name='Can Delete', codename='can_delete_something',
                       content_type=somemodel_ct)


can_delete.save()

但这对我来说似乎很难看,还有其他办法(除了去管理页面我想避免)

1 个答案:

答案 0 :(得分:0)

您可以在模型中声明自定义权限:

<div class="col-xs-12 col-sm-8 col-md-6 col-lg-4">