简体   繁体   English

自定义用户模型不适用于django admin

[英]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' 除此之外,我所做的更改在INSTALLED_APPS中添加了身份验证,并在设置中添加了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. 当我从shell创建一个超级用户时,它可以在db中工作并插入数据库,但是当我尝试登录django管理控制台时,最初找不到is_staff错误,这是我添加的。 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 我正在添加用于验证是否已创建用户的dbs转储

sqlite> select * from authentication_account;
1|pbkdf2_sha256$24000$ZRHCXpUI3w0G$fh4z9y5vmYAZ0FEiDr908dJD3ezw62nm3lpkZxUi/Es=||me@rahulbhola.in|||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 仍然登录Django defaut admin无法正常工作

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. 稍后添加了is_staff ,默认情况下它似乎为false,因此在迁移时,该字段是在is_staff字段中以false值在数据库中创建的。

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. 请不要在上面的代码中使用account.save(using = self.db),在create admin方法中请不要使用account.is_staff = True。 Please try creating the new user again i prefer using the shell ( python manage.py shell) 请尝试再次创建新用户,我更喜欢使用shell(python manage.py shell)

from authentication.models import Account 从authentication.models导入帐户

user = Account() 用户= Account()

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

user.save() user.save()

Now that the user is created please try logging in using the django admin. 现在已经创建了用户,请尝试使用django admin登录。 This should work. 这应该工作。 I had similar issues which is fixed and i am able to create user and login from django admin. 我有类似的问题已修复,我可以从django admin创建用户并登录。

I am assuming that you have created the custom authentication backend already, if not please create 我假设您已经创建了自定义身份验证后端,如果没有,请创建

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM