My First Image Of Code is here
我的模型在这里,我在我的类中使用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()
字符串
This Is Code Of 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'
1条答案
按热度按时间pftdvrlh1#
错误日志的最后一部分,
“TypeError:UserManager.create_superuser()missing 1 required positional argument:'username'”,表示用户名字段丢失。
方法'create_superuser'是用username调用的,你应该用相同的参数和关键字参数覆盖该方法以避免此类错误。
在django/contrib/auth中的原始UserManager如下。
字符串
因此,create_superuser方法应该如下所示。
型