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:
- The required fields changes according to my code
- 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 - 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.