georoot
georoot

Reputation: 3617

Custom user model not working on django admin

I created an app authentication, code for models is,

from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import BaseUserManager
from django.db import models

class AccountManager(BaseUserManager):
    def create_user(self, email, password=None, **kwargs):
        if not email:
            raise ValueError('Users must have a valid email address.')

        account = self.model(email=self.normalize_email(email))

        account.set_password(password)
        account.save()

        return account

    def create_superuser(self, email, password, **kwargs):
        account = self.create_user(email, password, **kwargs)

        account.is_admin = True
        account.save()
        return account


class Account(AbstractBaseUser):
    email = models.EmailField(unique=True)
    # username = models.CharField(max_length=40, unique=True)

    first_name = models.CharField(max_length=40, blank=True)
    last_name = models.CharField(max_length=40, blank=True)

    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    objects = AccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __unicode__(self):
        return self.email

    def get_full_name(self):
        return ' '.join([self.first_name, self.last_name])

    def get_short_name(self):
        return self.first_name

apart from this the changes that i did was added authentication in INSTALLED_APPS and in settings added AUTH_USER_MODEL = 'authentication.Account'

When i create a super-user from shell, its working and inserting in db, but when i try to login into the django admin console, initially the error was is_staff not found, which i added. Now it is showing invalid username and password for all. I tried that a couple of time generating new one each time. What am i doing wrong ?

EDIT

I am adding the dbs dump that i used to validate if the user was created

sqlite> select * from authentication_account;
1|pbkdf2_sha256$24000$ZRHCXpUI3w0G$fh4z9y5vmYAZ0FEiDr908dJD3ezw62nm3lpkZxUi/Es=||[email protected]|||1|0|2016-05-28 21:15:59.292988|2016-05-28 21:15:59.415382

Still login in django defaut admin does not work

Upvotes: 0

Views: 1979

Answers (1)

Niraj Pathak
Niraj Pathak

Reputation: 66

You added the is_staff later on which seems to be false by default, so when you migrated the field was created in database with the value false in the is_staff field.

Please make the changes as below and try

class AccountManager(BaseUserManager):
    def create_user(self, email, password=None, **kwargs):
        if not email:
            raise ValueError('Users must have a valid email address.')

        account = self.model(email=self.normalize_email(email))

        account.set_password(password)
        account.save(using=self.db)

        return account

    def create_superuser(self, email, password, **kwargs):
        account = self.create_user(email, password, **kwargs)
        account.is_staff = True
        account.is_admin = True
        account.save(using=self.db)
        
        return account

Please not account.save(using=self.db) in the code above and account.is_staff = True in the create admin method. Please try creating the new user again i prefer using the shell ( python manage.py shell)

from authentication.models import Account

user = Account()

user.create_superuser(username='some_username', password='some_password')

user.save()

Now that the user is created please try logging in using the django admin. This should work. I had similar issues which is fixed and i am able to create user and login from django admin.

I am assuming that you have created the custom authentication backend already, if not please create

Hope this helps you.

Upvotes: 1

Related Questions