我做对了吗?我在Django 1.5中创建了一个自定义用户。我现在想要将一个完全不同类型的用户添加到名为WebUser
的混合中,允许非常简单地访问已注册的前端页面/公共用户。但是,每次我尝试时都会出现以下错误...
尝试添加此内容:
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'。将related_name参数添加到定义中 对于'user_permissions'。 accounts.participantuser:m2m的访问者 字段'groups'与相关的m2m字段'Group.user_set'发生冲突。添加一个 “groups”定义的related_name参数。 accounts.participantuser:m2m字段'user_permissions'的访问者 与相关的m2m字段'Permission.user_set'发生冲突。添加一个 “user_permissions”定义的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'
答案 0 :(得分:1)
如果在ForeignKey上使用related_name属性或 ManyToManyField,您必须始终为其指定唯一的反向名称 领域。这通常会导致抽象基类出现问题, 因为这个类的字段包含在每个孩子中 类,具有完全相同的属性值(包括 related_name)每次。
我看到的一种方法是将PermissionMixin
替换为您自己的类,但它可能会破坏更多,因为相关名称会发生变化。