我用CustomUser编写了一个自定义用户类-- models.py,主要遵循这里
import re
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.core import validators
from django.contrib.auth.models import (AbstractUser, PermissionsMixin,
UserManager)
class CustomUser(AbstractUser, PermissionsMixin):
"""
custom user, reference below example
https://github.com/jonathanchu/django-custom-user-example/blob/master/customuser/accounts/models.py
"""
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and '
'@/./+/-/_ characters'),
validators=[validators.RegexValidator(
re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), 'invalid')
])
email = models.EmailField(_('email address'), max_length=254)
create_time = models.DateTimeField(auto_now_add=True)
active = models.BooleanField() # if we can retrieval it from outside
objects = UserManager()
USERNAME_FIELD = 'username'
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def email_user(self, subject, message, from_email=None):
"""
Sends an email to this User.
"""
send_mail(subject, message, from_email, [self.email])
def __str__(self):
return self.username然后我在admin.py上注册了
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
# Register your models here.
class CustomUserAdmin(UserAdmin):
model = CustomUser
admin.site.register(CustomUser, CustomUserAdmin)但是,当我运行python manage.py createsuperuser来创建超级用户时,我得到了以下错误:
ERRORS:
myapp.CustomUser.is_superuser: (models.E006) The field 'is_superuser' clash
es with the field 'is_superuser' from model 'myapp.customuser'.发布于 2017-09-07 14:25:27
is_superuser字段是在PermissionMixin上定义的。但是,AbstractUser也是子类PermissionMixin,因此您实际上是从同一个类继承了两次。这会导致字段冲突,因为以前版本的Django不允许子类覆盖字段(最近的版本允许覆盖在抽象基类上定义的字段)。
您要么必须继承AbstractBaseUser和PermissionMixin,要么只能继承AbstractUser。AbstractUser定义了一些附加字段,包括username、is_staff和is_active,以及其他一些内容。
https://stackoverflow.com/questions/46093946
复制相似问题