python - Custom User Model Django TypeError: create_user() missing 1 required positional argument: 'username'
问题描述
I am creating a custom User model and I am getting an error when I create my super user for the first time. Any advice, please. I would ideally like to user BOTH my username
or email
for verification interchangeably, but this may be a question for another day.
I started initially with email as my username_field
but changed after a while. Please, can someone provide guidance, please? I have posted my models.py
file which sits inside the accounts app.
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
The error from shell is as below
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>
解决方案
查看您的代码:
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
推荐阅读
- svg - 计算 SVG viewBox 以显示完整图像?
- c# - 如何在 if 语句中使用占位符来访问属性
- python - 多处理多个执行器等待每个作业
- outlook - Outlook Web 加载项 - 通过 EWS 调用创建的文件夹未显示在 OWA 中。在 Outlook Windows 中工作正常。这可能是我的代码的问题吗?
- javascript - 有没有办法获取我的 Stack Exchange 统计信息?
- ionic-framework - How to solve Source path does not exist: resources/android/xml/network_security_config.xml
- javascript - 为什么我不能在另一台计算机上访问我的 API?
- r - RMarkdown 中的 Beamer 和目录
- each - 从谷歌表格中的查询中重复 n 次每一行
- python - 如何使用 Pandas/Dask 更快地读取压缩(.gz)文件?