如何创建Django中的权限?

时间:2011-08-12 10:01:51

标签: django django-permissions

以下内容引发DoesNotExist:例外:

from django.contrib.auth.models import Permission
Permission.objects.get(codename='add_eventmanager')

我应该手动创建此权限吗?

1 个答案:

答案 0 :(得分:3)

是的,你应该

ROLE_PERMS =  (
    ('manage_managers', 'Manage managers'),
    ('manage_roles', 'Manage roles'),
    ('manage_enterprise_config', 'Manage enterprise config'),
    ('manage_orga_imports', 'Manage organization imports'),
    ('consult_stats', 'Consult statistics'),
    ('manage_distribs', 'Manage distributions'),
    ('manage_docs', 'Manage documents'),
    ('consult_docs', 'Consult documents'),
    ('manage_employees', 'Manage employees'),
    ('consult_employees', 'Consult employees'),
    ('manage_employee_imports', 'Manage employee imports'),
    ('manage_settings', 'Manage settings'), )

class Role(models.Model):

    class Meta:
        ordering = ['name']
        verbose_name = _('role')
        verbose_name_plural = _('roles')
        permissions = ROLE_PERMS