gpt4 book ai didi

python - AUTH_USER_MODEL 指尚未安装的型号

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:56 24 4
gpt4 key购买 nike

我收到错误

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

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

我以前从未见过此错误,无法弄清楚这是什么原因。我已经在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)
...

最佳答案

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

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

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

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

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

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

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

django-admin startproject mysite

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

**第 2 步:**该教程说:要创建您的应用,请确保您位于与 manage.py 相同的目录中,然后输入以下命令:这是刚刚创建的目录,因此请转到:

cd mysite

如果你ls那么你会在这个目录中找到:名为 ma​​nage.py 的文件令人困惑的是,另一个名为 mysite

的目录

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

python manage.py startapp polls

现在 ls 显示这些文件:管理.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 项目时,您会更加清楚,并且您将不再因存在多个同名目录而感到困惑。

关于python - AUTH_USER_MODEL 指尚未安装的型号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26914022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com