Я получил TypeError: UserManager.create_superuser() missing 1 required positional argument: 'username'

Я хочу создать суперпользователя только из электронной почты и пароля, поэтому он должен создать суперпользователя без имени пользователя. Я использую пользовательскую модель пользователя, и когда я пытаюсь создать суперпользователя, я получаю эту ошибку TypeError: UserManager.create_superuser() missing 1 required positional argument: 'username'

Это мой файл manager.py

from django.contrib.auth.base_user import BaseUserManager


class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):

        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_staff', True)

        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True')

        return self._create_user(email, password, **extra_fields)

Это мой models.py

class User(AbstractUser)
    """
    Abstract User of django auth user model.
    """
    uuid = models.UUIDField(default=uuid.uuid4, unique=True, null=False)
    email = models.EmailField(_("email address"), unique=True)
    middle_name = models.CharField(max_length=120, blank=True)
    image = models.ImageField(upload_to='user_profile/', blank=True)
    department = models.CharField(blank=True, choices=Department.choices,
                                  max_length=2)
    designation = models.CharField(max_length=120, blank=True)
    last_logout = models.DateTimeField(null=True)
    is_superuser = models.BooleanField(default=False)
    view = models.TextField(blank=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import AbstractUser
from django.db import models
import uuid


class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, email, password=None, **extra_fields):
        """
        Create and save a user with the given email and password.
        """
        if not email:
            raise ValueError(_('The given email must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', False)
        extra_fields.setdefault('is_staff', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_staff', True)

        return self._create_user(email, password, **extra_fields)


class User(AbstractUser):
    """
    Custom User model where email is the unique identifier instead of username.
    """
    uuid = models.UUIDField(default=uuid.uuid4, unique=True, null=False, editable=False)
    email = models.EmailField(_('email address'), unique=True)
    middle_name = models.CharField(max_length=120, blank=True)
    image = models.ImageField(upload_to='user_profile/', blank=True)
    department = models.CharField(blank=True, choices=Department.choices, max_length=2)
    designation = models.CharField(max_length=120, blank=True)
    last_logout = models.DateTimeField(null=True)
    view = models.TextField(blank=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def __str__(self):
        return self.email


Вы используете для наследования класса User класс AbstractUser. Класс AbstractUser имеет обязательное поле username. Переопределите поле в классе User или заполните имя пользователя в UserManager, или не используйте наследование класса AbstractUser.

from django.contrib.auth.base_user import BaseUserManager


class CustomUserManager(BaseUserManager):
 
    def create_user(self, email, password, **extra_fields):

        if not email:
            raise ValueError(_("The Email must be set"))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):

        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", True)
        extra_fields.setdefault("is_active", 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(email, password, **extra_fields)



class AppUser(AbstractUser):

    username = None
    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["first_name", "last_name"]
    objects = CustomUserManager()

Попытка сделать поле имя пользователя Неполевым

Вернуться на верх