2013-11-28 60 views
0

我做了這件事嗎?我在Django 1.5中創建了一個定製用戶。我現在想要爲名爲WebUser的組合添加完全不同類型的用戶,從而可以非常簡單地訪問已註冊的前端頁面/公共用戶。然而,每一次我嘗試,我得到以下錯誤的時間...Django 1.5 m2m字段'組'衝突的自定義用戶

嘗試添加此:

class WebUser(AbstractEmailUser): 
    company = models.CharField(max_length=100) 

class Meta: 
    app_label = 'accounts' 

我得到這個:

accounts.companyuser:訪問者爲m2m字段'groups'與 相關的m2m字段'Group.user_set'發生衝突。將'related_name'參數添加到'groups'的 定義中。 accounts.companyuser:m2m字段的訪問者 'user_permissions'與相關的m2m字段 'Permission.user_set'發生衝突。爲'user_permissions'定義 添加一個related_name參數。 accounts.participantuser:m2m 字段'groups'的訪問者與相關m2m字段'Group.user_set'發生衝突。 related_name參數添加到「組」的定義中。 accounts.participantuser:m2m字段的訪問者'user_permissions' 與相關的m2m字段'Permission.user_set'發生衝突。爲'user_permissions'的定義添加一個 related_name參數。

這是我的完整模型的工作版本之前,我嘗試添加新用戶:如果您使用的是related_name屬性

class EmailUserManager(BaseUserManager): 

    def create_user(self, email, password=None, **extra_fields): 
     """ 
     Creates and saves an EmailUser with the given email and password. 
     """ 
     now = timezone.now() 
     if not email: 
      raise ValueError('The given email must be set') 
     email = EmailUserManager.normalize_email(email) 
     user = self.model(email=email, is_staff=False, is_active=True, 
          is_superuser=False, last_login=now, 
          date_joined=now, **extra_fields) 

     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_superuser(self, email, password, **extra_fields): 
     """ 
     Creates and saves a superuser with the given email and password. 
     """ 
     user = self.create_user(email, password, **extra_fields) 
     user.is_staff = True 
     user.is_active = True 
     user.is_superuser = True 
     user.save(using=self._db) 
     return user 


class AbstractEmailUser(AbstractBaseUser, PermissionsMixin): 
    """ 
    Abstract User with the same behaviour as Django's default User but 
    without a username field. Uses email as the USERNAME_FIELD for 
    authentication. 

    Use this if you need to extend EmailUser. 

    Inherits from both the AbstractBaseUser and PermissionMixin. 

    The following attributes are inherited from the superclasses: 
     * password 
     * last_login 
     * is_superuser 
    """ 
    email = models.EmailField(_('email address'), max_length=255, 
           unique=True, db_index=True) 
    is_staff = models.BooleanField(_('staff status'), default=False, 
     help_text=_('Designates whether the user can log into this admin ' 
        'site.')) 
    is_active = models.BooleanField(_('active'), default=True, 
     help_text=_('Designates whether this user should be treated as ' 
        'active. Unselect this instead of deleting accounts.')) 
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now) 

    objects = EmailUserManager() 

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = [] 

    class Meta: 
     abstract = True 

    def get_full_name(self): 
     """ 
     Returns the email. 
     """ 
     return self.email 

    def get_short_name(self): 
     """ 
     Returns the email. 
     """ 
     return self.email 

    def email_user(self, subject, message, from_email=None): 
     """ 
     Sends an email to this User. 
     """ 
     send_mail(subject, message, from_email, [self.email]) 


class CompanyUser(AbstractEmailUser): 
    """ 
    Concrete class of AbstractEmailUser. 

    """ 

    company = models.CharField(max_length=100) 

    class Meta: 
     app_label = 'accounts' 
+1

不要以爲你可以有多個自定義'用戶'模型。 – Rohan

回答

1

看的文檔進行abstract base classes

在外鍵或 ManyToManyField上,必須始終爲 字段指定唯一的反向名稱。這通常會導致抽象基類 中的問題,因爲此類中的字段包含在每個子類 中,每次屬性的值都完全相同(包括 related_name)。

並看看PermissionMixin code

我看到的一種方法是用您自己的班級替換PermissionMixin,但由於相關名稱會改變,所以它可能會破壞更多。

+0

我看到謝謝。正確的,然後回到繪圖板。我有點用戶有自己的用戶,這是行不通的。我只想要一個客戶用戶模型,但是網絡用戶在主要用戶下,即他們的用戶,如果這是有意義的話 – GrantU

相關問題