1

I created my own user model and usermanager model. Then I tried to create a supersuser and get "Superuser created successfully." However, when I tried to login with all the info, it failed. Then I added a line of code to print my password while creating. The result matches with my password.I am pretty sure the server is using the custom model because:

  1. The required fields changes according to my code
  2. If I remove one necessary field from the required fields, I get an error about "TypeError: create_superuser() missing 1 required positional argument:" which is expected, so it is in the right way
  3. I used .count() method to get the user in the database and i can see the amount increase with 1 after I created one

Here is my code:

class UserManager(BaseUserManager):
    def create_user(self, username,email,password=None):
        if not email:
            raise ValueError("Users must have an email address")
        if not password:
            raise ValueError("Users must have a password")
        user_obj = self.model(
            email=self.normalize_email(email)
        )
        user_obj.set_password(password)
        user_obj.username = username
        user_obj.save(using=self.db)
        return user_obj

    def create_superuser(self, username, email, password):
        print("Super user created, with password" + password)
        return self.create_user(
            username=username,
            email=email,
            password=password,
        )

class User(AbstractBaseUser):
    username = models.CharField(max_length=255, unique=True)
    email = models.EmailField(blank=True, unique=True)
    created_time = models.TimeField(auto_now=True)
    active = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
    objects = UserManager()

I really appreciate if anybody can give me any idea. Totally get lost.

2 Answers 2

1

I think you should change:

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

to:

def create_superuser(self, username, email, password, **extra_fields):
    ....
    return self._create_user(username, email, password, **extra_fields)

because the parameter of **extra_fields is required in function of _create_user also create_user, you can checkout at this source.

def _create_user(self, username, email, password, **extra_fields):
    ....

def create_user(self, username, email=None, password=None, **extra_fields):
    ....
Sign up to request clarification or add additional context in comments.

4 Comments

is that extra_fields an array?
and why don't you also include the fields username, email, password in the extrafield? Because they are required?
nope, here is Understanding kwargs in Python ... I hope you check at the source to see why extra_fields needed.
Thank you so much!
0

The solution to this situation is set is_superuser and is_staff to True, then program will give the account permission to access to admin site

Comments

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.