Python Django 1.5 m2m字段的自定义用户';组';冲突
我做对了吗?我在Django 1.5中创建了一个自定义用户,该用户可以正常工作。现在,我想在名为Python Django 1.5 m2m字段的自定义用户';组';冲突,python,django,django-models,Python,Django,Django Models,我做对了吗?我在Django 1.5中创建了一个自定义用户,该用户可以正常工作。现在,我想在名为WebUser的组合中添加一种完全不同的用户类型,允许非常简单地访问已注册的前端页面/公共用户。但是,每次尝试时,我都会出现以下错误 尝试添加以下内容: class WebUser(AbstractEmailUser): company = models.CharField(max_length=100) class Meta: app_label = 'accounts' cla
WebUser
的组合中添加一种完全不同的用户类型,允许非常简单地访问已注册的前端页面/公共用户。但是,每次尝试时,我都会出现以下错误
尝试添加以下内容:
class WebUser(AbstractEmailUser):
company = models.CharField(max_length=100)
class Meta:
app_label = 'accounts'
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'
我明白了:
accounts.companyuser:m2m字段“组”的访问器与冲突
相关m2m字段“组用户集”。将相关的_name参数添加到
“组”的定义。accounts.companyuser:m2m字段的访问器
“用户权限”与相关m2m字段冲突
“权限。用户设置”。将相关的_name参数添加到定义中
对于“用户权限”。accounts.participantuser:m2m的访问者
字段“groups”与相关m2m字段“Group.user\u set”冲突。添加
与“组”定义相关的\u name参数。
accounts.participantuser:m2m字段“用户权限”的访问者
与相关m2m字段“Permission.user_set”冲突。添加
与“用户权限”定义相关的\u name参数
这是我尝试添加新用户之前的完整模型工作版本:
class WebUser(AbstractEmailUser):
company = models.CharField(max_length=100)
class Meta:
app_label = 'accounts'
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'
查看文档以了解更多信息
如果您正在ForeignKey上使用相关的_name属性,或
ManyToManyField,您必须始终为
领域这通常会导致抽象基类出现问题,
因为这个类上的字段包含在每个子类中
类,属性的值完全相同(包括
每次都有相关的名称
看看
我看到的一种方法是用您自己的类替换
PermissionMixin
,但它可能会中断更多,因为相关名称将发生更改。不要认为您可以有多个自定义用户
模型。我看到了,谢谢。好的,回到绘图板上。我有点希望我的用户有自己的用户,这是行不通的。我只想要一个客户-用户模型,但web用户位于主用户之下,即他们的用户,如果这有任何意义的话