我有一个自定义的User
模型在我的models.py
和自定义的UserManager
以及。我的自定义User
模型有username
字段,名称更改为login
,相同的更新在UserManager
以及。一切正常,但是当我尝试使用命令python manage.py createsuperuser
创建超级用户时,它要求login
,但它没有要求email
,最后给了我这个错误:
TypeError: UserManager.create_superuser() missing 1 required positional argument: 'email'
字符串
我的自定义User
模型是:
class User(AbstractBaseUser, PermissionsMixin):
login = models.CharField(max_length=254, unique=True)
email = models.EmailField(max_length=254, unique=True)
first_name = models.CharField(max_length=254, null=True, blank=True)
last_name = models.CharField(max_length=254, null=True, blank=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'login'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def get_absolute_url(self):
return "/users/%i/" % (self.pk)
型
我的自定义UserManager
是:
class UserManager(BaseUserManager):
def _create_user(self, login, email, password, is_staff, is_admin, **extra_fields):
if not email:
raise ValueError('Users must have an email address')
now = timezone.now()
email = self.normalize_email(email)
user = self.model(
login=login,
email=email,
is_staff=is_staff,
is_active=True,
is_admin=is_admin,
last_login=now,
date_joined=now,
**extra_fields
)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, login, email, password, **extra_fields):
user = self._create_user(login, email, password, False, False, **extra_fields)
user.save(using=self._db)
return user
def create_superuser(self, login, email, password, **extra_fields):
user=self._create_user(login, email, password, True, True, **extra_fields)
return user
型
在admin.py
中,我有:
class UserAdmin(BaseUserAdmin):
fieldsets = (
(None, {'fields': ('login', 'email', 'password', 'first_name', 'last_name', 'last_login')}),
('Permissions', {'fields': (
'is_active',
'is_staff',
'is_admin',
'groups',
'user_permissions',
)}),
)
add_fieldsets = (
(
None,
{
'classes': ('wide',),
'fields': ('login', 'email', 'password1', 'password2')
}
),
)
list_display = ('login', 'email', 'first_name', 'last_name', 'is_admin', 'last_login')
list_filter = ('is_staff', 'is_admin', 'is_active', 'groups')
search_fields = ('email', 'login',)
ordering = ('email', 'login',)
filter_horizontal = ('groups', 'user_permissions',)
型
2条答案
按热度按时间ldfqzlk81#
我只需要在
User
模型定义的REQUIRED_FIELDS
变量中添加'email'
。即我不得不改变以下行:
字符串
至
型
zd287kbt2#
你需要在你的应用程序中创建一个新表单,然后将其引用到你的admin.py模块中。
forms.py
字符串
然后进入您的admin.py
型