Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Creating a super user User

In the create_superuser method(), how to make that the user to create to be super user, and staff? I guess, with the user.is_staff and user.is_superuser is enough, but the question I do not work ...

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:
            raise ValueError("First message")
        # Make new user, user instance in memory
        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 = accepted_tos
        )
        user.set_password(password)
        # handle the encryption and validation checks and so.
        user.save()
        return user

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

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Bernardo,

I think you have mixed up the video lesson with the code challenge a little bit. Double check the values that the create_superusers method takes: it should take only email dob and password. The same thing in self.createuser() doublecheck what you are passing in.

Last thing: the 4th last line should be user.accepted_tos = True

Let me know if this helps ;)

Vitto