0

I'm using Django 2.0

I have extended the AbstractBaseUser to make some modification to the email and use email as username

class UserManager(BaseUserManager): def create_user(self, email, password=None, is_staff=False, is_superuser=False): if not email: raise ValueError('User must have an email address') if not password: raise ValueError('User must have a password') user = self.model( email=self.normalize_email(email) ) user.is_staff = is_staff user.is_superuser = is_superuser user.set_password(password) user.save(using=self._db) return user def create_staffuser(self, email, password=None): return self.create_user( email, password=password, is_staff=True ) def create_superuser(self, email, password=None): return self.create_user( email, password=password, is_staff=True, is_superuser=True ) class User(AbstractBaseUser): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) email = models.EmailField(max_length=250, blank=False, unique=True) first_name = models.CharField(max_length=150, blank=True) last_name = models.CharField(max_length=150, blank=True) is_staff = models.BooleanField(default=False) is_active = models.BooleanField(default=False) is_superuser = models.BooleanField(default=False) groups = models.ManyToManyField(Group, blank=True) last_login = models.DateTimeField(auto_now=True) date_joined = models.DateTimeField(auto_now_add=True) USERNAME_FIELD = 'email' objects = UserManager() @property def staff(self): return self.is_staff @property def active(self): return self.is_active @property def superuser(self): return self.is_superuser def __str__(self): if self.first_name is not None: return self.get_full_name() return self.email def get_full_name(self): if self.last_name is not None: return self.first_name + ' ' + self.last_name return self.get_short_name() def get_short_name(self): return self.first_name 

I have then run

python manage.py makemigrations python manage.py migrate 

and created superadmin using

python manage.py createsuperuser 

The superuser is created successfully.

But when I login using 127.0.0.1:8000/admin/

I gives

Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive. 

What is wrong with the code?

1 Answer 1

2

You need to define has_perm and has_module_perms methods:

def has_perm(self, perm, obj=None): "Does the user have a specific permission?" # Simplest possible answer: Yes, always return True def has_module_perms(self, app_label): "Does the user have permissions to view the app `app_label`?" # Simplest possible answer: Yes, always return True 
Sign up to request clarification or add additional context in comments.

3 Comments

I added those methods. But now on makemigrations it is giving django.core.exceptions.FieldError: Unknown field(s) (is_active, is_staff) specified for User Error. I do have added both fields in the model as in question
@AnujTBE does makemigrations work without this methods?
well I have added some code to admin.py to add this model to admin. And the error is there in admin.py. I will post a different question for this as my current question has been answered.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.