3

假设我有一个项目,我事先知道我将只有三个用户组。我可以提前从 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 的默认权限功能相比有什么优势,如果我打算将它与这些模型一起使用,我应该记住什么?谢谢!

4

1 回答 1

2

简单的元组加法怎么样?

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 只是一个 mixin。如果 GroupParent 是 Django 模型,则必须使用GroupParent._meta.permissions.

于 2012-11-12T15:52:13.043 回答