提问人:Hamza Bilal 提问时间:11/15/2023 最后编辑:TemiHamza Bilal 更新时间:11/17/2023 访问量:23
当我从管理员更新用户信息时,会出现此错误
When I Update The User Info From Admin Then This error Come
问:
我的模型在这里,我在课堂上使用AbstractUser
class CustomMyUserModel(AbstractUser):
username = None
phone_number = models.CharField(max_length=100, unique=True)
email = models.EmailField(unique=False)
user_bio = models.CharField(max_length=50)
user_profile_image = models.ImageField(upload_to='upload')
USERNAME_FIELD = 'phone_number'
REQUIRED_FIELDS = []
objects = UserManager()
这是代码Migrations.py
from django.contrib.auth.base_user import BaseUserManager
class UserManager(BaseUserManager):
def create_user(self, phone_number, password=None, **extra_fields):
if not phone_number:
raise ValueError('The Phone Number Is required:')
extra_fields.setdefault('email', self.normalize_email(extra_fields.get('email')))
user = self.model(phone_number=phone_number, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, phone_number, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(phone_number, password=password, **extra_fields)
(overrideuser) hamza@hamza-HP-EliteBook-840-G1:~/PycharmProjects/pythonProject2/OverrideUser$ python manage.py createsuperuser
运行上述命令会引发以下结果:error
Phone number: 030476701645 Email: [email protected] Password: Password (again):
Traceback (most recent call last):
File "/home/hamza/PycharmProjects/pythonProject2/OverrideUser/manage.py", line 22, in <module>
main()
File "/home/hamza/PycharmProjects/pythonProject2/OverrideUser/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/hamza/PycharmProjects/pythonProject2/overrideuser/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/home/hamza/PycharmProjects/pythonProject2/overrideuser/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/hamza/PycharmProjects/pythonProject2/overrideuser/lib/python3.10/site-packages/django/core/management/base.py", line 412, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/hamza/PycharmProjects/pythonProject2/overrideuser/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 88, in execute
return super().execute(*args, **options)
File "/home/hamza/PycharmProjects/pythonProject2/overrideuser/lib/python3.10/site-packages/django/core/management/base.py", line 458, in execute
output = self.handle(*args, **options)
File "/home/hamza/PycharmProjects/pythonProject2/overrideuser/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 233, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(
TypeError: UserManager.create_superuser() missing 1 required positional argument: 'username'
答:
0赞
Donghyun Lee
11/15/2023
#1
错误日志的最后一部分,
“TypeError: UserManager.create_superuser() missing 1 required positional argument: 'username'”,表示缺少用户名字段。
“create_superuser”方法是使用 username 调用的,您应该使用相同的参数和关键字参数覆盖该方法以避免此类错误。
在原来的 UserManager 中,django/contrib/auth 如下。
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, username, email, password, **extra_fields):
"""
Create and save a user with the given username, email, and password.
"""
if not username:
raise ValueError("The given username must be set")
email = self.normalize_email(email)
# Lookup the real model class from the global app registry so this
# manager method can be used in migrations. This is fine because
# managers are by definition working on the real model.
GlobalUserModel = apps.get_model(
self.model._meta.app_label, self.model._meta.object_name
)
username = GlobalUserModel.normalize_username(username)
user = self.model(username=username, email=email, **extra_fields)
user.password = make_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(username, email, password, **extra_fields)
def create_superuser(self, username, email=None, password=None, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")
return self._create_user(username, email, password, **extra_fields)
因此,您的create_superuser方法应如下所示。
def create_superuser(self, username, phone_number, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(username, phone_number, password=password, **extra_fields)
评论