0
class Task(models.Model):

    class Meta:
        permissions = (
            ("view_task", "Can see available tasks"),
            ("change_task_status", "Can change the status of tasks"),
            ("close_task", "Can remove a task by setting its status as closed"),
        )
user.has_perm('app.view_task')

如果我有 contenttype.codename unknown(not hardcoded) 有什么方法可以检查权限,即它来自数据库动态如下

permission = Permission.objects.get(id=item.permission_id )
contenttype = ContentType.objects.get(id=permission.content_type_id)               
user.has_perm(appname.contenttype.codename)

但它总是返回 false

4

1 回答 1

0

尝试

user.has_perm(u'%s.%s' % (contenttype.app_label, permission.codename))

另外,使用

contenttype = ContentType.objects.get_for_model(Permission)
于 2012-05-01T07:39:43.860 回答