在Django UserProfile模型中继承组权限

时间:2012-11-12 13:17:58

标签: django django-models django-permissions

假设我有一个项目,我事先知道我只有三个用户组。我可以事先从GroupParent继承来定义它们,然后使用这些组选项向我的UserProfile添加一个字段吗?这基本上就是我想做的事情:

class GroupParent(??):
    class Meta:
        permissions = (
            ('do_something', 'Can do something'),
            ('edit_task', 'Can edit task'),
     );

class GroupAdmin(GroupParent):
    class Meta:
        // I want to inherit the permissions from the Mixin and add a few
        // I know this would only overwrite... how do I do it?
        permissions = (
            ('do_fancy_tasks', 'Can do fancy tasks'),
         )

class GroupCoordinator(GroupParent):
     class Meta:
     // inherit from GroupParent and add my own
         permissions = (
            ('open_file', 'Can open files'),
         )

class GroupNormal(GroupParent):
     class Meta:
     // inherit from GroupParent and add my own
         permissions = (
            ('open_file', 'Can open files'),
            ('edit_file', 'Can edit files'),
         )

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    has_manager = models.BooleanField()
    ...
    ...
    group = models.CharField(max_length=2, choices=GROUP_CHOICES) // how??

这是一种合理的方法,还是应该在构建应用程序后(或者在构建应用程序时)放弃并设置组+权限?

另外......使用django-guardian而不是django的默认权限功能有什么好处?如果我打算在这些模型中使用它,我应该记住什么?谢谢!

1 个答案:

答案 0 :(得分:1)

简单的元组添加怎么样?

class GroupParent(object):
    class Meta:
        permissions = (
            ('do_something', 'Can do something'),
            ('edit_task', 'Can edit task'),
        )

class GroupAdmin(GroupParent):
    class Meta:
        permissions = GroupParent.Meta.permissions + (
            ('do_fancy_tasks', 'Can do fancy tasks'),
         )

这是好的,因为GroupParent只是一个混合。如果GroupParent是Django模型,则必须使用GroupParent._meta.permissions