0

I am using an abstractbase user. When I run python manage.py createsuperuser, it successfully created it but always gets that error and I don't know where is the problem. And also I put in the required fields the username and the birth date as I want normal users to register with them besides the email. however, I don't want the admin to enter these fields when I use createsuperuser command, Is there a way to do it?

class UserManager(BaseUserManager):

    def create_user(self,email,password, birth_date,username):

        if not email:
            raise ValueError("Email must be set")
        email = self.normalize_email(email)
        user = self.model(email=email,birth_date=birth_date, username=username)
        user.set_password(password)
        user.save()
        #user.save(using=self._db)
        return user

    def create_superuser(self,email,password,birth_date,username,**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 set to True')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser set to True')
        return self.create_user(email,password,birth_date,username)


class User(AbstractUser):
    username = models.CharField(max_length=20, null=True, blank=True)
    email = models.EmailField(verbose_name='email address',max_length=255,unique=True)
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    birth_date = models.CharField(max_length=10)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['birth_date','username',]

    objects = UserManager()

    def __str__(self):
        return self.username

1 Answers1

0

edit this:

return self.create_user(email,password,birth_date,username)

to this:

return self.create_user(email,password,username)

and remove birth_date from this:

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

try this and let me know if this worked or not

  • yes it worked but also I forgot to set is_admin and all the other permissions si now it is working but if I remove birth_Date it will be missing an argument so it won't work, however I don't need the admin to register with the birth date only the users shall do. – Menna T-Allah Magdy Aug 02 '21 at 10:30