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 trialHenrik Christensen
Python Web Development Techdegree Student 38,322 Pointssuperuser
Hi,
I'm trying to make a custom user model, but every time I try to log in (no matter how I do it) with the new superuser it says Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive..
I'm currently trying to use the example showed by Kenneth Love but I'm still getting this "not staff account".
Here is my code.
settings.py
AUTH_USER_MODEL = 'profiles.User'
models.py
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser, PermissionsMixin
)
from django.db import models
from django.utils import timezone
class UserManager(BaseUserManager):
def create_user(self, username, email, password=None, display_name=None):
if not email:
raise ValueError('Users must have an email address!')
if not display_name:
display_name = username
user = self.model(
username=username,
email=self.normalize_email(email),
display_name=display_name,
)
user.set_password(password)
user.save()
return user
def create_superuser(self, username, email, password, display_name):
user = self.create_user(
email,
username,
password=password,
display_name=display_name,
)
user.is_staff = True
user.is_superuser = True
user.save()
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
username = models.CharField(max_length=40, unique=True)
first_name = models.CharField(max_length=70, blank=True, default='')
last_name = models.CharField(max_length=70, blank=True, default='')
display_name = models.CharField(max_length=140)
bio = models.CharField(max_length=150, blank=True, default='')
date_joined = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['display_name', 'username']
def __str__(self):
return '@{}'.format(self.username)
def get_short_name(self):
return self.display_name
def get_full_name(self):
return '{} (@{})'.format(self.display_name, self.username)
3 Answers
Kenneth Love
Treehouse Guest TeacherI'm looking through the docs and your example and I don't see anything that stands out as wrong. Do you have another user you can get into the admin with? Maybe check that the new user you're creating has is_staff
set correctly (you could always select the user in the Django shell, too).
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsHi Kenneth Love,
I managed to fix the problem by saying email=email, and swapping around email and username, in the create_superuser method - I have no idea why that worked, but it's working now! :-D
def create_superuser(self, username, email, password, display_name):
user = self.create_user(
username, # put this first otherwise the terminal spits out some nasty errors
email=email, # email=email instead of just email
password=password,
display_name=display_name,
)
user.is_staff = True
user.is_superuser = True
user.save()
return user
Kenneth Love
Treehouse Guest TeacherWeird.
My only guess is that it has something to do with having the email address as the username field? I'm really not sure, though.
Umesh Chaudhary
Courses Plus Student 746 Pointsdjango.core.exceptions.FieldDoesNotExist: User has no field named 'email'
i am getting this error
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsHenrik Christensen
Python Web Development Techdegree Student 38,322 PointsI just tried to create a new user in the shell and set it to is_staff = True, but I'm still getting same error + I just noticed that I can't even do a normal login /profile/login due to the same error :-/