Python Django-“ AUTH_USER_MODEL指的是尚未安装的模型'%s'”%设置。AUTH_USER_MODEL

时间:2019-05-13 09:49:44

标签: python django mixins

如果我的代码中有任何错误(在使用LoginRequiredMixin的视图中使用的模型中的代码),例如像这样的错误:

if (True:    # <-- example bug to show how bugs like this are hidden

然后我得到以下错误:

"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 'auth.User' that has not been installed

这使得调试代码非常困难。我已经确定是否可以从views.py中删除此行:

from django.contrib.auth.mixins import LoginRequiredMixin

即使我删除了所有使用LoginRequiredMixin的实例,仅通过导入它也会隐藏真正的错误并生成上述身份验证错误。

我已阅读:AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL

我的研究表明,大多数情况下,这是由于INSTALLED_APPS中缺少“ django.contrib.auth'”所致,您可以看到我的名字(settings.py片段,Django 2.2):

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth', # <-- Important for this topic!
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp.apps.CommonConfig',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

我尝试将其添加到mixim导入上方,但都没有帮助:

from django.contrib import auth
from django.contrib.auth.models import User

完整追溯的这一部分与上面的观点相同:

Traceback (most recent call last):
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\apps\registry.py", line 155, in get_app_config
    return self.app_configs[app_label]
KeyError: 'auth'

如果我调用makemigrations(这是我期望的),这是回溯:

    C:\code\myproject\src>pipenv run python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\code\myproject\src\myapp\models.py", line 14, in <module>
    from .sql import myapp_entry_clean, myapp_entry_grouping
  File "C:\code\myproject\src\myapp\sql.py", line 68
    if (True:
            ^
SyntaxError: invalid syntax

C:\code\myproject\src>

这是运行服务器的回溯:

    C:\code\myproject\src>pipenv run python manage.py runserver
Watching for file changes with StatReloader
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\Lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\Lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\utils\autoreload.py", line 77, in raise_last_exception
    raise _exception[0](_exception[1]).with_traceback(_exception[2])
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\code\myproject\src\myapp\models.py", line 14, in <module>
    from .sql import myapp_entry_clean, myapp_entry_grouping
  File "<string>", line None
SyntaxError: invalid syntax (sql.py, line 68)

Traceback (most recent call last):
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\apps\registry.py", line 155, in get_app_config
    return self.app_configs[app_label]
KeyError: 'auth'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\contrib\auth\__init__.py", line 165, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\apps\registry.py", line 205, in get_model
    app_config = self.get_app_config(app_label)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\apps\registry.py", line 162, in get_app_config
    raise LookupError(message)
LookupError: No installed app with label 'auth'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\commands\runserver.py", line 95, in handle
    self.run(**options)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\core\management\commands\runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\utils\autoreload.py", line 579, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\utils\autoreload.py", line 564, in start_django
    reloader.run(django_main_thread)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\utils\autoreload.py", line 272, in run
    get_resolver().urlconf_module
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\utils\functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\urls\resolvers.py", line 564, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\code\myproject\src\linkcube\urls.py", line 20, in <module>
    path('', include('common.urls')),
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\code\myproject\src\common\urls.py", line 5, in <module>
    from . import views
  File "C:\code\myproject\src\common\views.py", line 9, in <module>
    from django.contrib.auth.mixins import LoginRequiredMixin
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\contrib\auth\mixins.py", line 3, in <module>
    from django.contrib.auth.views import redirect_to_login
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\contrib\auth\views.py", line 10, in <module>
    from django.contrib.auth.forms import (
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\contrib\auth\forms.py", line 20, in <module>
    UserModel = get_user_model()
  File "C:\Users\User\.virtualenvs\myproject-wc-xNQPL\lib\site-packages\django\contrib\auth\__init__.py", line 170, 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 'auth.User' that has not been installed

C:\code\myproject\src>

我如何做到这一点,以便不会错误地引发auth错误?

4 个答案:

答案 0 :(得分:1)

所以这里的问题是python语法错误,但是(也许)由于您正在运行Django框架,因此Django会抑制该错误并抛出缺少的模型错误。

由于python是解释性语言,因此它为SyntaxError引发了错误,并且由于存在语法错误,Django无法找到auth模型引用。

这就是为什么您同时看到python SyntaxError和Django ImproperlyConfigured错误的原因。

Django非常友好,可以让您知道Django的错误是由python的语法错误引起的。

答案 1 :(得分:1)

如果收到该错误消息,我相信您正在使用自定义用户模型。 您可能想在关系上使用 AUTH_USER_MODEL ,而不是直接模型 User get_user_model 方法。这几乎为我清除了错误。 我从https://learndjango.com/tutorials/django-best-practices-referencing-user-model

获得了解决方案

使用 AUTH_USER_MODEL 的方法是,首先使用from django.conf import settings从设置中将其导入,然后在需要关联的字段上将其用作seller = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

答案 2 :(得分:0)

如果您不使用任何自定义用户模型,则应删除

AUTH_USER_MODEL='auth.User'

来自setting.py

也请在此处修复

if (True:

if True:
    ...

答案 3 :(得分:-1)

在我删除Sqlite数据库和用户迁移文件并意外删除用户应用程序models.py之后,在使用Django 3.0时发生了这种情况。否则看看:

AUTH_USER_MODEL refers to model 'accounts.User' that has not been installed