django 当我从管理员更新用户信息时,出现此错误

h9vpoimq  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(101)

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'


My 2nd Image Of Code is here

pftdvrlh

pftdvrlh1#

错误日志的最后一部分,
“TypeError:UserManager.create_superuser()missing 1 required positional argument:'username'”,表示用户名字段丢失。
方法'create_superuser'是用username调用的,你应该用相同的参数和关键字参数覆盖该方法以避免此类错误。
在django/contrib/auth中的原始UserManager如下。

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)

相关问题