CreateSuperUser ask for an id instead of email

Hello Guys,
OverView:
i made a main Account model which have OneToOneFields and ForeignKeyFields too for a minor models Email - Name - Slug, and i made the AccounrManager Class too with create_user and create_superuser methods,
Makemigrations and Migrate is done correctly without Errors,
but when i try to python manage.py createsuperuser it asks for the Email.id and not for the email itself, Also i made the Identifier for Account model is email as USERNAME_FIELD = gettext_lazy("email")

will be grateful for any idea or any direction.

Code:

class AccountManager(BaseUserManager):
    def create_user(self, email, first_name, last_name, password=None):
        if not email:
            raise ValueError(gettext_lazy("E-Mail is Required, Please Provide Your E-Mail."))
        if not first_name:
            raise ValueError(gettext_lazy("E-Mail is Required, Please Provide Your First Name."))
        if not last_name:
            raise ValueError(gettext_lazy("E-Mail is Required, Please Provide Your Last Name."))
        user = self.model(email=self.normalize_email(email).lower(), first_name=first_name, last_name=last_name)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, first_name, last_name, password=None):
        user = self.create_user(email=self.normalize_email(email), first_name=first_name, last_name=last_name,
                                password=password)
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user

class Email(models.Model):
    email = models.EmailField(max_length=255, unique=True)

    class Meta:
        verbose_name = gettext_lazy("Email")
        verbose_name_plural = gettext_lazy("Emails")

    def __str__(self):
        return f"{self.email}"


class Name(models.Model):
    name = models.CharField(max_length=255, )

    class Meta:
        verbose_name = gettext_lazy("Name")
        verbose_name_plural = gettext_lazy("Names")

    def __str__(self):
        return f"{self.name}"


class Slug(models.Model):
    slug = models.SlugField(max_length=500, unique=True)

    class Meta:
        verbose_name = gettext_lazy("Slug")
        verbose_name_plural = gettext_lazy("Slugs")

    def __str__(self):
        return f"{self.slug}"

class Account(AbstractBaseUser, PermissionsMixin):
    email = models.OneToOneField(
        Email,
        related_name="email_account",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        unique=True,
        db_index=True
    )
    first_name = models.ForeignKey(
        Name,
        related_name="first_name_account",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        db_index=True
    )
    last_name = models.ForeignKey(
        Name,
        related_name="last_name_account",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        db_index=True
    )
    slug = models.OneToOneField(
        Slug,
        related_name="slug_account",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        db_index=True
    )

    is_active = models.BooleanField(verbose_name=gettext_lazy("Is Active"), default=True)
    is_staff = models.BooleanField(verbose_name=gettext_lazy("Is Staff"), default=False)
    is_superuser = models.BooleanField(verbose_name=gettext_lazy("Is SuperUser"), default=False)
    date_joined = models.DateTimeField(verbose_name=gettext_lazy("Date Joined"), auto_now_add=True)
    last_login = models.DateTimeField(verbose_name=gettext_lazy("Last Login"), auto_now=True)

    USERNAME_FIELD = gettext_lazy("email")
    REQUIRED_FIELDS = [gettext_lazy("first_name"), gettext_lazy("last_name"), ]

    objects = AccountManager()

That makes sense because email is a foreign key to a different model.

If you want different behavior, you’ll need to create your own createsuperuser command.

1 Like

Why i keep getting this Error,

Error:

Unknown command: 'custom_createsuperuser'. Did you mean createsuperuser?
Type 'manage.py help' for usage.

I created Directories management/commands under my root directory, and added "management.commands", into my installed apps and my custom command file called custom_createsuperuser.py ,

is there anything i missed?

See the docs at How to create custom django-admin commands | Django documentation | Django.

The management directory needs to be inside an app’s directory. They don’t exist at the project root.

1 Like