【问题标题】:django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'auth.User' that has not been installeddjango.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL 指的是尚未安装的模型'auth.User'
【发布时间】:2016-07-05 12:19:49
【问题描述】:

在迁移我的 django 和 userena 包后,如下所示

Django 1.8 到 Django 1.9.7

django-userena 1.4.1 到 django-userena==2.0.1

运行项目后,我遇到了这个错误

Unhandled exception in thread started by <function wrapper at 0xb689641c>
Traceback (most recent call last):
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/Documents/swamy/sample_project/july/5/sample11/sampleapp/urls.py", line 28, in <module>
(r'^grappelli/', include('grappelli.urls')),
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/grappelli/urls.py", line 8, in <module>
from .views.switch import switch_user
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/grappelli/views/switch.py", line 18, in <module>
User = get_user_model()
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 155, 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 指的是尚未安装的模型'auth.User'

这是我的设置文件中的 INSTALLED_APPS,

'grappelli.dashboard',
'grappelli',
'filebrowser',     
'django.contrib.admindocs',
'django.contrib.admin',
'django.contrib.auth',    
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',  
'django.contrib.redirects',
'django.contrib.sitemaps',
'haystack',  
'memcache_status',   
'stheme',    
'home',
'customers',
'orders',
#'legacy',
'products',
'bloglets',
'utils',
'catax',
'sqls',
'quotes',    
#'django_stylus',
#'djgrid',
#'obdjects',
'quickpages', 
'loginas',
#'pyjade',
'django_countries',  
'debug_toolbar',
'djide',
#'dbtemplates',  
#'aloha',  # out temporarily, migrate to alternate https://github.com/ntucker/django-aloha-edit - JJW
'coffeescript',
'django_wysiwyg',
#'django_bfm',
'userena',
'guardian', 
#'apps',  
#'filer',
'easy_thumbnails', 
'taggit',
#'taggit_templatetags',
# 'social_auth',    
'social.apps.django_app.default', 
#'socialregistration',
#'socialregistration.contrib.linkedin',
'email_extras',
#'csvimport', 
'csvimport.app.CSVImportConf',  
'django_extensions',
'webshell',
'easy_select2',  
#'plata',
#'plata.contact', 
#'plata.discount',
#'plata.payment',
#'plata.shop',
'lastmodule',

我猜python应用程序有一些变化。但我找不到原因......有人帮忙解决这个问题吗?

提前致谢!

【问题讨论】:

  • 尝试 ./manage.py migrate auth 先迁移 auth 包,然后再迁移 ./manage.py migrate?
  • 对我来说这是 installed_apps 列表中的语法错误,缺少一个逗号

标签: django django-guardian django-userena


【解决方案1】:

如果您的应用程序未调用 auth,则必须替换它:
AUTH_USER_MODEL='your_app_name.User'

【讨论】:

  • 只需将模型从 UserProfile 替换为 User 即可解决我的问题
  • 实际上是app_label 而不是app_name,它们是两个不同的东西。
【解决方案2】:

这是因为您有一个应用“auth”,您已在其中定义了用户模型。 并且您没有在 INSTALLED_APPS 字典中提及应用名称。

尝试在 INSTALLED_APPS 中添加您的应用名称并检查。

【讨论】:

    【解决方案3】:

    当我在 v-2.2 中遇到同样的问题时,我意识到 admin.py 文件中“register”的拼写是错误的。

    【讨论】:

      【解决方案4】:

      尝试先进行迁移; python manage.py makemigrations。这可能会检测到您的导入是否有任何问题,您可以修复这些问题,然后运行服务器进行验证

      【讨论】:

      • 这个答案与问题无关。运行任何 python manage.py 将给出相同的错误,而无需任何额外的上下文。
      【解决方案5】:

      完整的回溯将有助于更好地诊断它。从表面上看,在我看来,这是由于迁移引起的依赖问题。检查 Django 文档对此有何评论 -

      由于 Django 的动态依赖特性的限制 可交换模型,您必须确保所引用的模型 AUTH_USER_MODEL 是在其应用程序的第一次迁移中创建的(通常 称为 0001_initial);否则,您将遇到依赖问题。

      这是链接 - https://docs.djangoproject.com/en/1.9/topics/auth/customizing/

      【讨论】:

      • 谢谢你,我会尝试一次,我会告诉你
      【解决方案6】:

      这个问题通常存在于两个原因之间。

      1. 当已安装应用中的依赖顺序颠倒时。
      2. 当您根本没有提到已安装应用程序中的依赖关系时。

      在这种情况下,grappelli 似乎提出了告诉auth.User 未找到的问题。这意味着它无法找到任何包auth。如果您使用的是默认用户模型,请从配置中删除 AUTH_USER_MODEL 设置,或者如果您在包“auth”中使用任何自定义用户模型,请将其列在已安装的应用程序中。

      【讨论】:

      • 太棒了。为我工作,问题是已安装应用程序的排序。
      【解决方案7】:

      由于在 admin.py(Django 版本 2.2)中缺少模型导入,也可能出现此错误。

      【讨论】:

        【解决方案8】:

        我怀疑这是典型的答案,但是当我使用 reverse 并且应该使用 reverse_lazy 时遇到了这个问题。更改为 reverse_lazy 为我修复了它。

        【讨论】:

          【解决方案9】:

          当我的模型配置不正确并将其添加到管理后端时,会出现此错误。

          Wrong:
             class VolunteerExperience:
           ...
          
          Correct:
             class VolunteerExperience(models.Model):
           ...
          

          我希望这对被异常消息 "django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'auth.User' that has not been installed" 误导的人有所帮助

          现在我知道此消息很可能表明模型配置不正确。

          【讨论】:

            【解决方案10】:

            除了所有其他答案之外,我在升级 Django 时遇到了这个问题。我有一个从一个版本到另一个版本不推荐使用的导入。请务必在升级任何软件包之前检查发行说明/版本历史记录。

            https://docs.djangoproject.com/en/dev/releases/

            【讨论】:

              猜你喜欢
              • 2022-08-15
              • 1970-01-01
              • 2015-01-10
              • 1970-01-01
              • 1970-01-01
              • 2021-10-07
              • 2021-07-12
              • 2019-06-27
              • 1970-01-01
              相关资源
              最近更新 更多