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.

Jieke Wei
  • 173
  • 2
  • 13

2 Answers2

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):
    ....
binpy
  • 3,994
  • 3
  • 17
  • 54
  • is that extra_fields an array? – Jieke Wei Nov 21 '17 at 10:15
  • and why don't you also include the fields username, email, password in the extrafield? Because they are required? – Jieke Wei Nov 21 '17 at 10:16
  • nope, here is [Understanding kwargs in Python](https://stackoverflow.com/questions/1769403/understanding-kwargs-in-python) ... I hope you check at the [source](https://github.com/django/django/blob/master/django/contrib/auth/models.py#L134-L161) to see why `extra_fields` needed. – binpy Nov 21 '17 at 10:34
  • Thank you so much! – Jieke Wei Nov 21 '17 at 10:39
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

Jieke Wei
  • 173
  • 2
  • 13