Python 在AUTH\u User\u模型中使用继承AbstractBaseUser类的自定义用户时未注册该类
为了让我的自定义用户类作为Django CMS 1.8.8中的默认类工作,我已经挣扎了一个多小时 我创建了一个名为Python 在AUTH\u User\u模型中使用继承AbstractBaseUser类的自定义用户时未注册该类,python,django,django-models,Python,Django,Django Models,为了让我的自定义用户类作为Django CMS 1.8.8中的默认类工作,我已经挣扎了一个多小时 我创建了一个名为authentication的新应用程序,在models.py文件中添加了我的自定义用户模型帐户和自定义管理器 from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from django.db import models class AccountManager(BaseUserMana
authentication
的新应用程序,在models.py文件中添加了我的自定义用户模型帐户
和自定义管理器
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models
class AccountManager(BaseUserManager):
def _create_user(self, username, email, password, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
if not username:
raise ValueError('The given username must be set')
email = self.normalize_email(email)
account = self.model(username=username, email=email, **extra_fields)
account.set_password(password)
account.save(using=self._db)
return account
def create_user(self, username, email=None, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(username, email, password, **extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(username, email, password, **extra_fields)
class Account(AbstractBaseUser):
username = models.CharField(
max_length=150,
unique=True,
help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.',
error_messages={
'unique': "A user with that username already exists.",
},
)
first_name = models.CharField(max_length=60, blank=True)
last_name = models.CharField(max_length=60, blank=True)
email = models.EmailField(unique=True, blank=True) # if login with twitter, no email address is passed
tagline = models.CharField(max_length=200, blank=True)
is_staff = models.BooleanField(
default=False,
help_text='Designates whether the user can log into this admin site.',
)
is_active = models.BooleanField(
default=True,
help_text=(
'Designates whether this user should be treated as active. '
'Unselect this instead of deleting accounts.'
),
)
created_at = models.DateField(auto_now_add=True)
updated_at = models.DateField(auto_now=True)
objects = AccountManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def __unicode__(self):
return self.username
def __str__(self):
return self.username
def get_full_name(self):
"""
Returns the first_name plus the last_name, with a space in between.
"""
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
"""Returns the short name for the user."""
return self.first_name
def has_perm(self, perm, obj=None):
"""Does the user have a specific permission?"""
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"""Does the user have permissions to view the app `app_label`?"""
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"""Is the user a member of staff?"""
# Simplest possible answer: All admins are staff
return self.is_admin
我将应用程序添加到mysite settings.py的INSTALLED_app
:
INSTALLED_APPS = (
'djangocms_admin_style',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib.messages',
'cms',
'menus',
'sekizai',
'treebeard',
'djangocms_text_ckeditor',
'djangocms_style',
'djangocms_column',
'djangocms_file',
'djangocms_googlemap',
'djangocms_inherit',
'djangocms_link',
'djangocms_picture',
'djangocms_teaser',
'djangocms_video',
'reversion',
'mysite',
'authentication'
)
在底部,我写道
AUTH\u USER\u MODEL='authentication.Account'
我得到了LookupError:Model'authentication.Account'未注册。
但是,去掉这一行,我就能够创建一个迁移,迁移。当我访问管理员时,我可以正确地添加一个新的帐户
它快把我逼疯了。我真的不明白为什么它不是在配置中找到类,而是在显示管理UI时找到它。这对我来说毫无意义…我认为你的应用程序名为“身份验证”,与django.contrib.auth中的身份验证冲突
此外,根据文件:
更改AUTH\u USER\u模型对数据库结构有很大影响。它会更改可用的表,并且会影响外键和多对多关系的构造。如果要设置AUTH_USER_模型,应在创建任何迁移或首次运行manage.py migrate之前进行设置
不支持在创建表后更改此设置
通过makemigrations,将导致您必须手动修复
架构,从旧用户表移植数据,可能还需要手动移植
重新应用一些迁移
您应该确保AUTH_USER_model引用的模型是在其应用程序的第一次迁移中创建的(通常称为0001_initial)我发现了这个问题。几乎在互联网上的任何地方,人们都在将应用程序添加到已安装的应用程序的末尾,但实际上,我在一开始就添加了应用程序,它正在工作
INSTALLED_APPS = (
'authentication',
'djangocms_admin_style',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib.messages',
'cms',
'menus',
'sekizai',
'treebeard',
'djangocms_text_ckeditor',
'djangocms_style',
'djangocms_column',
'djangocms_file',
'djangocms_googlemap',
'djangocms_inherit',
'djangocms_link',
'djangocms_picture',
'djangocms_teaser',
'djangocms_video',
'reversion',
'rest_framework',
'mysite',
)
编辑:经过一些思考,这是因为我在搜索时混淆了Django和Django CMS。在Django CMS中查找该问题时,您会发现您的应用程序必须添加到INSTALLED_APPS
列表中的CMS
应用程序之前。
(嗯,我不能接受我的答案是正确的,有人可以这样做吗?您是否运行了makemigrations
?makemigrations
抛出了模型未注册错误。基本上,任何manage.py命令都会抛出错误。同样的问题,我用相同的模型创建了一个custom\u身份验证
app。关于迁移,我甚至不能创建它,因为它会直接抛出错误,不会进一步。我认为你应该从头开始。删除数据库,因为它以前已经迁移过。请记住:“更改AUTH_USER_模型对数据库结构有很大影响。”因此,应该首先进行更改。确保设置与我们商定的一致。AUTH\u USER\u MODEL='custom\u authentication.Account'。帐户模型中没有拼写错误,是AbstractUser的子类。然后尝试只迁移到自定义_身份验证。我发现了问题,我回答了我的问题。谢谢你的帮助:)