Django LookupError:应用'帐户'没有'用户'模型

时间:2017-08-20 14:24:53

标签: python django

从Django 1.10升级到1.11后,我在做任何事情时都遇到了问题。我无法启动服务器,也无法运行manage.py。

在升级之前,我没有收到此问题,因此我不确定自那时以来会发生什么变化会导致此问题。

我也已经尝试删除数据库,没有任何改变。

我的settings.py AUTH_USER_MODEL / INSTALLED_APPS:

AUTH_USER_MODEL = 'accounts.User'

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts.apps.AccountsConfig',
    'forum.apps.ForumConfig',
]

我的用户模型:

class User(AbstractUser):
    profile_picture = models.ImageField(null=True, blank=True)
    birth_date = models.DateField(null=True, blank=True)

错误:

> Failed to get real commands on module "CsForum": python process died with code 
1: Traceback (most recent call last):
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 169, in get_model
    return self.models[model_name.lower()]
KeyError: 'user' 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\__init__.py", line 193, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 205, in get_model
    return app_config.get_model(model_name, require_ready=require_ready)
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 172, in get_model
    "App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'accounts' doesn't have a 'User' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2017.2.1\helpers\pycharm\_jb_manage_tasks_provider.py", line 25, in <module>
    django.setup()
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models()
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\Users\Elian\OneDrive\PycharmProjects\CsForum\accounts\models.py", line 1, in <module>
    from django.contrib.auth.admin import UserAdmin
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\admin.py", line 7, in <module>
    from django.contrib.auth.forms import (
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\forms.py", line 22, in <module>
    UserModel = get_user_model()
  File "C:\Users\Elian\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\__init__.py", line 198, in get_user_model
    "AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'accounts.User' that has not been installed

5 个答案:

答案 0 :(得分:4)

我设法解决了这个问题。问题是我升级到1.11后,我的CustomUserAdmin(管理页面中自定义字段的扩展UserAdmin)不能再出现在我的models.py中。它现在在我的admin.py中,问题不再发生。

答案 1 :(得分:0)

我有一个类似的问题,我把CustomModelBackend放在models.py

将其置于单独的backends.py中解决了问题

答案 2 :(得分:0)

只需在'updatedWDABundleId': updated_WDA_Bundle_Id, 'agentPath': "/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent/WebDriverAgent.xcodeproj", 'bootstrapPath': "/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent" 文件中注册所有模型。就你而言 admin.py

--Admin.py--

对我有用。

答案 3 :(得分:0)

您应该在安装应用程序后注册用户模型。 应用程序应该在模型之前初始化,因为模型在应用程序中。

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts.apps.AccountsConfig',
    'forum.apps.ForumConfig',
]
AUTH_USER_MODEL = 'accounts.User'

答案 4 :(得分:0)

尝试创建名为MyUser自定义用户和代理模型(使用电子邮件地址作为用户名)时,我遇到了同样的问题。避免此错误的最佳实践是在运行任何迁移之前创建您的自定义用户对我来说,解决方案是:

  • 重置所有迁移

转到您的项目迁移文件夹,然后删除期望__init__.py中的所有内容。在Linux或GitBash上运行:

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
  • 删除数据库,或删除 db.sqlite3 (如果您正在使用的话)。

如果db.sqlite3数据库中已经有文本信息(例如用户信息),并且您希望在删除数据库之前拥有if。在项目目录中创建文件backup.py(不必担心在运行后将其删除)并粘贴以下代码以备份您的文本信息

import sqlite3
connection = sqlite3.connect('db.sqlite3')  # or the name of your sqlite db
cursor = connection.cursor()

app_name = "your_app" # replace with the name of your app
tables = []

for table_tuple in cursor.execute("SELECT name FROM sqlite_master WHERE type='table'"):
  for table_name in table_tuple:
    if app_name in table_name:
      tables.append(table_name)

data = ''
for table in tables:
  rows = ''
  for row in cursor.execute("SELECT * FROM %s" %table):
    rows += "\t%s,\n"%str(row)
  data += '\n%s=[\n%s\n]\n'%(table,rows[0:-2])

with open('backup.txt', 'w') as my_file:
  my_file.write(data)

运行该文件,您的数据将驻留在另一个文件backup.txt中。

  • 再次创建迁移并生成数据库架构。

像往常一样在我们的项目目录中运行:

python manage.py makemigrations
python manage.py migrate

现在,您的问题应该已经解决,您可以重新开始添加数据。对于SQLite用户,您可以查看将数据添加到数据库但正在运行的语法:

cursor.executemany ("INSERT INTO your_database_table_name VALUES (?,?,?,?)", data)

(?,?,?,?)是表中的列数。如果弄错了-没问题,数据库将提示您所需的行数。

基本上,您可以通过正确的调整复制上述脚本中最后一个backup.txt文件中保存的每个数据。