3
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager

# Create your models here.

class MyAccountManager(BaseUserManager):
    def create_user(self, email, username, password=None):
        if not email:
            raise ValueError('user must have smartcard')
        if not username:
            raise ValueError('must have username')

        user = self.create_user(
            email=self.normalize_email(email),
            username=username,

        )

        user.set_password(password)
        user.save(using=self._db)
        return user

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


class Account(AbstractBaseUser):

    username = models.CharField(max_length=100, unique=True)

    email = models.EmailField(verbose_name="email", max_length=30, unique=True)
    department = models.CharField(max_length=30, choices=DEPARTMENT)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELD = ['username',]

    objects=MyAccountManager()

    def __str__(self):
        return self.email   

    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return True   

I just can't get what is wrong.

While creating a superuser the prompt doesnt ask me for entering username

I modified the settings.py file as well. Tried several times with various databases. I have also added the username in REQUIRED_FIELDS section.

Eliakin Costa
  • 930
  • 6
  • 16
Omkar Kale
  • 33
  • 1
  • 3
  • Include the stack trace in your question. – aaron Dec 13 '19 at 06:21
  • Does this answer your question? [Issue with createsuperuser when implementing custom user model](https://stackoverflow.com/questions/25239164/issue-with-createsuperuser-when-implementing-custom-user-model) – aaron Dec 13 '19 at 07:49

2 Answers2

2

Welcome to SO, @Omkar.

You just need to change the fields REQUIRED_FIELD to REQUIRED_FIELDS.

REQUIRED_FIELDS = ['username']

You can check it on the docs.

Eliakin Costa
  • 930
  • 6
  • 16
2

@Omkar Kale, Please look your method:

def create_superuser(self, email, username, password):
    user = self.create_user(
        email=self.normalize_email(email),
        password=password,
        username=username,
    )

Please change it:

 def create_superuser(self, username, email, password):

    user = self.create_user(
        username=username,
        email=self.normalize_email(email),
        password=password,

        )

Look, you need to pass value according to the parameter. So

  • first: username,
  • second: email
  • third password.

Then you will no longer going to receive error for positional argument. you have to pass them according to the position of the parameter.

Iqbal Hussain
  • 1,077
  • 1
  • 4
  • 12
  • This doesn't solve the problem in the question, but is good advice in general — consistent order of arguments. – aaron Dec 13 '19 at 06:33