2012-11-12 74 views
2

假设我有一个项目,我事先知道我只会有三个用户组。我可以预先定义它们继承从一个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的监护人在Django的默认权限的能力和我应该记住什么,如果我打算用这些模型使用它呢?谢谢!

回答

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