[Fixed]-Custom user model not working on django admin

1👍

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.

Leave a comment