2012-11-12 120 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