Python Django Authentication Users and Authorization Custom User Manager

Bernardo Augusto García Loaiza
PRO
Bernardo Augusto García Loaiza
Pro Student 792 Points

Superuser isn't marked as being staff

I get the message "Superuser isn't marked as being staff" when the method create_superuser is executed:

Why is happened this?

accounts/models.py
from django.contrib.auth.models import BaseUserManager

class UserManager(BaseUserManager):
    def create_user(self, email, dob, accepted_tos=None, password=None):
        if not accepted_tos or accepted_tos is None:
            raise ValueError("Users must accept the terms")
        user  = self.model(
            # make sure that all the email addresses throughout your app are formatted the same way
            email = self.normalize_email(email),
            dob = dob,
            accepted_tos = True
        )
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, dob, password):
        user = self.create_user(
            email,
            dob,
            accepted_tos,
            password,
        )
        user.is_staff = True
        user.accepted_tos = True
        user.is_superuser = True
        user.save()
        return user

1 Answer

You're really close, actually. Because you're tossing the values up to the create_user function you need to set your accepted_tos = True inside your self.create_user call rather than after it. If you don't, then when it hits the if-check in your create_user it will default to None. Also, you should also be setting your password = password inside the call as well, since you don't want blank password superusers running around.