我正在创建自定义用户模型,并且在首次创建超级用户时遇到错误。请提供任何建议。理想情况下,我想同时使用我的username
或email
进行验证,但这可能是又一天的问题了。
我最初以电子邮件username_field
的身份开始,但过了一会儿就改变了。请,有人可以提供指导吗?我已经发布了models.py
文件,该文件位于帐户应用中。
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser,
BaseUserManager
)
class UserManager(BaseUserManager):
def create_user(self, email,
username,
password=None,
is_active=True,
is_staff=False,
is_admin=False,
is_client=True, # anyone registered can be a client
is_partner=False
):
if not username:
raise ValueError("Unique username is required")
if not email:
raise ValueError("User must have an email address")
if not password:
raise ValueError("Password is required")
user_obj = self.model(
email=self.normalize_username(username)
)
user_obj.set_password(password) # change user password
user_obj.username = username
user_obj.email = email
user_obj.staff = is_staff
user_obj.client = is_client
user_obj.partner = is_partner
user_obj.admin = is_admin
user_obj.active = is_active
user_obj.save(using=self._db)
return user_obj
def create_staff_user(self, username, password=None):
user = self.create_user(
username,
password=password,
is_staff=True
)
return user
def create_partner_user(self, username, password=None):
user = self.create_user(
username,
password=password,
is_partner=True
)
return user
def create_client_user(self, username, password=None):
user = self.create_user(
username,
password=password,
is_client=True
)
return user
def create_superuser(self, username, password=None):
user = self.create_user(
username,
password=password,
is_staff=True,
is_admin=True
)
return user
class User(AbstractBaseUser):
username = models.CharField(unique=True, max_length=255)
full_name = models.CharField(max_length=255, blank=True, null=True)
email = models.EmailField(unique=True, max_length=255)
active = models.BooleanField(default=True) # can login
staff = models.BooleanField(default=False) # staff user non super
admin = models.BooleanField(default=False) # superuser
timestamp = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'username' # username
REQUIRED_FIELDS = [] #['full_name']
objects = UserManager()
def __str__(self):
return self.username
def get_full_name(self):
return self.username
def get_short_name(self):
return self.username
@property
def is_staff(self):
return self.staff
@property
def is_partner(self):
return self.partner
@property
def is_client(self):
return self.client
@property
def is_admin(self):
return self.admin
@property
def is_active(self):
return self.active
shell出现的错误如下
PS C:\Users\User\Desktop\protectandserve> python manage.py createsuperuser
Username: xxxxxxxx
Password:
Password (again):
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\contrib\auth\management\commands\createsuperuser.py", line 59, in execute
return super().execute(*args, **options)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\contrib\auth\management\commands\createsuperuser.py", line 184, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "C:\Users\User\Desktop\protectandserve\accounts\models.py", line 68, in create_superuser
is_admin=True
TypeError: create_user() missing 1 required positional argument: 'username'
PS C:\Users\User\Desktop\protectandserve>
答案 0 :(得分:1)
查看您的代码:
def create_superuser(self, username, password=None):
user = self.create_user(
username,
password=password,
is_staff=True,
is_admin=True
)
return user
您没有设置username
。应该是:
def create_superuser(self, username, password=None):
user = self.create_user(
username = username,
password=password,
is_staff=True,
is_admin=True
)
return user
此外,您可以尝试通过电子邮件和用户名注册进行此操作:
def create_superuser(self, username=None, email=None, password=None):
user = self.create_user(
username = username,
email = email.
password=password,
is_staff=True,
is_admin=True
)
return user
答案 1 :(得分:0)
如果您需要将两个字段都作为必填字段
REQUIRED_FIELDS = ['username', 'email' ]