Django管理员无法使用自定义用户模型正确登录

问题描述:

我将Django 1.4.5上的应用程序升级到Django 1.5,并刚刚迁移到自定义用户模型.当我使用自己的身份验证表单和我的超级用户凭据(在执行manage.py syncdb时创建的)登录到我的应用程序时,一切正常.

I upgraded an app I had on Django 1.4.5 to Django 1.5 and just finished migrating over to a custom User model. When I login to my app, using my own authentication form, with my superuser credentials (created when doing manage.py syncdb) everything works fine.

我能够获得身份验证,如果我进入/admin,则我已经按照预期登录.我能够导航和完美使用管理控制台.但是,如果我尝试使用django管理员登录表单从/admin登录到管理面板,则会收到错误消息:

I am able to get authenticated and if I go to /admin, I am already logged in, as expected. I am able to navigate and use the Admin panel perfectly. However, if I try to login to the admin panel from /admin, using the django admin login form, I get the error:

请为员工帐户输入正确的电子邮件和密码.笔记 这两个字段都可能区分大小写.

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

我做了一些调查,认为可能与ModelAdmin有关,所以我遵循了

I did some investigating and thought it could have something to do with ModelAdmin, so I followed this example from the docs and created a custom ModelAdmin. However, the problem still persists.

有什么想法会导致这种情况吗?

Any ideas what could be causing this?

您是否在BaseUserManager下的create_superuser函数中添加了以下几行?可能看起来像这样:

Did you add following lines in to your create_superuser function which is under BaseUserManager? It might look like this:

class CustomUserManager(BaseUserManager):
    def create_user(self, username, email, password=None):
        if not username:
            raise ValueError('Dude you need a username.')
        if not email:
            raise ValueError(
                'type an e-mail..')

        new_user = self.model(
            username=username,
            email=CustomUserManager.normalize_email(email))
        new_user.set_password(password)
        new_user.save(using=self._db)
        return new_user

  def create_superuser(self, username, email, password):
        new = self.create_user(
            username,
            email,
            password=password
        )
        new.is_active = True
        new.is_staff = True
        new.is_superuser = True
        new.save(using=self._db)
        return new

关注:

    new.is_active = True
    new.is_staff = True
    new.is_superuser = True