AUTH_USER_MODEL 指的是没有安装的型号

新手上路,请多包涵

我收到一个错误

ImproperlyConfigured at /admin/
AUTH_USER_MODEL refers to model 'ledger.User' that has not been installed

我只在我的生产服务器上得到它。当我通过本地主机运行时不是。首先,只有当我提出某个要求时。然后我认为我的数据库一定是不同步的所以我删除了所有表然后运行 manage.py syncdb。现在,它似乎已经传播开来,甚至去管理员那里也会抛出错误。

我以前从未见过这个错误,也不知道这是怎么回事。我在 settings.py 中定义了 AUTH_USER_MODEL:

 ...
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ledger',
    'extension',
    'plugin',
    'social.apps.django_app.default',
)

AUTH_USER_MODEL = 'ledger.User'

...

模型.py:

 ...
class User(AbstractUser):
    def __unicode__(self):
        return self.username
    balance = models.IntegerField(default=0)
    total_pledged = models.IntegerField(default=0)
    last_pledged = models.ForeignKey('Transaction', related_name='pledger', blank=True, null=True)
    extension_key = models.CharField(max_length=100, null=True, blank=True)
    plugin_key = models.CharField(max_length=100, null=True, blank=True)
    ghosted = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        print('saving')
        try:
            self.company.save()
        except:
            print('no company')
        super(User, self).save(*args, **kwargs)
...

原文由 Chase Roberts 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 600
1 个回答

我遇到了这个问题,通过正确理解 Django 文件的结构解决了这个问题。

教程中的说明通常各不相同且令人困惑。

你需要明白,当你安装Django时,有两个关键步骤:

1:创建项目 2:创建app(应用)

让我们按照 Django 官方教程来说明问题:

https://docs.djangoproject.com/en/3.1/intro/tutorial01/

第一步:创建一个新项目:

 django-admin startproject mysite

您现在会发现有一个名为“mysite”的目录

**第 2 步:**教程说: 要创建您的应用程序,请确保您与 manage.py 位于同一目录中并键入此命令: 这是刚刚创建的目录,因此请转到:

 cd mysite

如果你 ls 然后你会发现在这个目录中是:一个名为 manage.py 的文件令人困惑,另一个名为 mysite 的目录

第 3 步: 现在教程说要创建您的 Django 应用程序:

 python manage.py startapp polls

所以现在 ls 显示这些文件:manage.py

我的网站

投票

Consufion 现在可能已经设置好了,因为所有这些文件都在 mysite 目录下。

第 4 步: 如果您想使用自定义用户模型,那么官方的建议是在项目开始时就做,然后再进行任何迁移。

好的,所以编辑 mysite/settings.py 并添加以下行:

AUTH_USER_MODEL = ‘polls.User’

并编辑民意调查/模型并添加:

 from django.db import models

# Create your models here.

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass

\*\* 第 5 步:** 所以现在应该可以进行第一次迁移了,对吧?

 python manage.py makemigrations

但是啪!

 Traceback (most recent call last):
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/apps/registry.py", line 156, in get_app_config
    return self.app_configs[app_label]
KeyError: 'polls'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 157, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/apps/registry.py", line 206, in get_model
    app_config = self.get_app_config(app_label)
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/apps/registry.py", line 163, in get_app_config
    raise LookupError(message)
LookupError: No installed app with label 'polls'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/admin/apps.py", line 24, in ready
    self.module.autodiscover()
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/auth/admin.py", line 6, in <module>
    from django.contrib.auth.forms import (
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/auth/forms.py", line 21, in <module>
    UserModel = get_user_model()
  File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 161, in get_user_model
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'polls.User' that has not been installed
(venv3.8) ubuntu@ip-172-26-5-79:~/mysite$

存在错误:.ImproperlyConfigured:AUTH_USER_MODEL 指的是尚未安装的模型“polls.User”

所以我们通过修改 mysite/settings.py 来解决这个问题:

 INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

对此:

 INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
]

请注意,您需要添加的行是“polls”,这是您的 Django 应用程序的名称。

现在再试一次:

 (venv3.8) ubuntu@ip-172-26-5-79:~/mysite$ python manage.py makemigrations
Migrations for 'polls':
  polls/migrations/0001_initial.py
    - Create model User
(venv3.8) ubuntu@ip-172-26-5-79:~/mysite$

成功!!!

所以这个长篇故事的重点是要明确 Django 必须知道你的 Django 应用程序在哪里。您告诉 Django 的地方是 settings.py 文件中的 INSTALLED_APPS。

这确实混淆了 Django 项目和 Django 应用程序之间的区别,并且由于奇怪的建议创建两个具有相同名称的目录而变得更糟。

相反,为了让事情更清楚,我建议您在 Django 项目名称后加上“project”后缀,在 Django 应用程序名称后加上“app”后缀,不要给顶级目录与项目同名。

因此要建立一个新项目:

 (venv3.8) ubuntu@ip-172-26-5-79:~$ mkdir container
(venv3.8) ubuntu@ip-172-26-5-79:~$ cd container/
(venv3.8) ubuntu@ip-172-26-5-79:~/container$ django-admin startproject myproject .
(venv3.8) ubuntu@ip-172-26-5-79:~/container$ ls
manage.py  myproject
(venv3.8) ubuntu@ip-172-26-5-79:~/container$ python manage.py startapp myapp
(venv3.8) ubuntu@ip-172-26-5-79:~/container$ ls -lah
total 20K
drwxrwxr-x  4 ubuntu ubuntu 4.0K Oct 27 05:30 .
drwxr-xr-x 11 ubuntu ubuntu 4.0K Oct 27 05:29 ..
-rwxrwx---  1 ubuntu ubuntu  665 Oct 27 05:30 manage.py
drwxrwxr-x  3 ubuntu ubuntu 4.0K Oct 27 05:30 myapp
drwxrwxr-x  3 ubuntu ubuntu 4.0K Oct 27 05:30 myproject
(venv3.8) ubuntu@ip-172-26-5-79:~/container$

现在,当您处理 Django 站点和 Django 项目时,您会更加清楚,并且不会再因存在多个同名目录而感到困惑。

原文由 Duke Dougal 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题