【问题标题】:Django LookupError: App 'accounts' doesn't have a 'User' modelDjango LookupError:应用程序“帐户”没有“用户”模型
【发布时间】:2018-01-28 16:54:18
【问题描述】:

从 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

【问题讨论】:

  • 你试过在INSTALLED_APPS之后定义AUTH_USER_MODEL吗?
  • 是的,我试过了,但没有解决办法

标签: python django


【解决方案1】:

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

【讨论】:

【解决方案2】:

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

将它放在单独的 backends.py 中解决了这个问题

【讨论】:

    【解决方案3】:

    只需在admin.py 文件中注册您的所有模型。在你的情况下 --Admin.py--

    admin.site.register(User)
    

    它对我有用。

    【讨论】:

      【解决方案4】:

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

      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'
      

      【讨论】:

        【解决方案5】:

        当我尝试创建一个名为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 文件中保存的每个数据,并进行正确调整。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-09
          • 1970-01-01
          • 2020-06-19
          相关资源
          最近更新 更多