Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Authentication Users and Authorization Custom User Manager

Bernardo Augusto García Loaiza
PLUS
Bernardo Augusto García Loaiza
Courses Plus 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.