我做了這件事嗎?我在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'
不要以爲你可以有多個自定義'用戶'模型。 – Rohan