Search code examples
pythondjangodjango-modelsdjango-authenticationdjango-auth-models

login with superuser nog working in django admin panel


So I built this custom auth model because I want to use the email instead of the username field which is default for django.

AUTH_USER_MODEL = 'customers.Customers'

This I have in my settings.py

from django.utils import timezone
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager
from django.contrib.auth.models import UserManager
# Create your models here.    
class CustomerManager(UserManager):
    def _create_user(self, email, password, **extra_fields):
        if not email:
            raise ValueError('Customers must have an email address')
        user = self.model(
            email=email,
            **extra_fields
        )
        user.set_password(password)
        user.save(using=self._db)
        return user
    
    def create_user(self, email=None, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', False)
        extra_fields.setdefault('is_staff', False)
        return self._create_user(email, password, **extra_fields)
    
    def create_superuser(self, name, last_name, email, phone, password, **kwargs):
        kwargs.setdefault('is_superuser', True)
        kwargs.setdefault('is_staff', True)
        
        return self._create_user(email, password, **kwargs)

class Customers (AbstractBaseUser, PermissionsMixin):
    name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    email = models.EmailField(blank=False, unique=True)
    phone = models.CharField(max_length=15)
    password = models.CharField(max_length=20)
    
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    
    date_joined = models.DateTimeField(default=timezone.now)
    last_login = models.DateTimeField(blank=True, null=True)
    objects = CustomerManager()
    
    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = ['name', 'last_name', 'phone']
    
    class Meta:
        verbose_name = 'Customer'
        verbose_name_plural = 'Customers'
        
    def get_full_name(self):
        return self.name + ' ' + self.last_name
    
    def get_short_name(self):
        return self.name
    
    def check_password(self, password):
        return self.password == password

This is my custom auth model. I have succesfully created the superuser but when trying to login I always get the error:

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

Im 100% sure i'm using the correct login credentials. Anybody knows what could be the problem? Looking forward to everyone's thoughts!

EDIT

I went in to the sqlite3 db and copied the password there and used it for authentication and it worked. I noticed it's a hash (probably done by django's user model) But is there something i'm missing because i used the correct password to login but maybe by default it doesnt hash when logging in? Or do I have to create a custom login function or something?


Solution

  • Remove password and is_superuser fields and check_password method from Customers model since you are already inheriting from AbstractBaseUser and PermissionsMixinclasses.

    class Customers (AbstractBaseUser, PermissionsMixin):
        name = models.CharField(max_length=20)
        last_name = models.CharField(max_length=20)
        email = models.EmailField(blank=False, unique=True)
        phone = models.CharField(max_length=15)
        
        is_active = models.BooleanField(default=True)
        is_staff = models.BooleanField(default=False)
        
        date_joined = models.DateTimeField(default=timezone.now)
        last_login = models.DateTimeField(blank=True, null=True)
        objects = CustomerManager()
        
        USERNAME_FIELD = 'email'
        EMAIL_FIELD = 'email'
        REQUIRED_FIELDS = ['name', 'last_name', 'phone']
        
        class Meta:
            verbose_name = 'Customer'
            verbose_name_plural = 'Customers'
            
        def get_full_name(self):
            return self.name + ' ' + self.last_name
        
        def get_short_name(self):
            return self.name