1

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"?

1 Answer 1

4

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'.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh thanks that worked! So does that mean the name of the parameters matter in Django?
They matter in Python in fact. See this docs, it explains better and i think that you can understand what was happening: tutorialspoint.com/python/python_functions.htm
Thanks for the link. But I still don't understand how is the parameters names important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.