whoAmI
whoAmI

Reputation: 368

Django custom user model gives error: TypeError: create_superuser() got an unexpected keyword argument 'username'

I'm creating a Django custom user based on a tutorial as below:

class UserProfileManager(BaseUserManager):
    """Helps Django work with our custom user model."""

    def create_user(self, email, name, password=None):
        """Creates a user profile object."""

        if not email:
            raise ValueError('Users must have an email address.')

        email = self.normalize_email(email)
        user = self.model(email=email, username=name)

        user.user_id = -1
        user.set_password(password)
        user.save(using=self._db)

        return user

    def create_superuser(self, email, name, password):
        """Creates and saves a new superuser with given details."""

        user = self.create_user(email = email, name = name, password = password)

        user.is_superuser = True

        user.save(using=self._db)


class CustomUser(AbstractBaseUser, PermissionsMixin):
    """Represents a user profile inside our system"""

    email = models.EmailField(max_length=255, unique=True)
    username = models.CharField(max_length=255, unique=True)
    user_id = models.IntegerField()

    objects = UserProfileManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

    def __str__(self):
        return self.email

And I get the following error when I try to create a superuser:

TypeError: create_superuser() got an unexpected keyword argument 'username'

However if I change the name of "username" field to "name", I will be able to create a superuser with no error! Does anyone know why can't I name the field anything other than "name"?

Upvotes: 1

Views: 2991

Answers (1)

Luan Fonseca
Luan Fonseca

Reputation: 1517

On your function you defined the username parameter as name:

...
def create_superuser(self, email, name, password):
    ...

And in some other part of your code or even inside django itself. Maybe someone is calling your create_superuser function with username='some-username'.

Upvotes: 4

Related Questions