我曾经遇到这个问题,通过正确理解Django字段的结构,最终得以解决。
教程中的指导通常存在差异且令人困惑。
你需要了解,在安装Django时有两个关键步骤:
1. 创建项目
2. 创建应用程序(app)
我们通过跟随官方Django教程来说明问题:
步骤1:创建新项目:
https://docs.djangoproject.com/en/3.1/intro/tutorial01/
注: 创建一个项目。
django-admin startproject mysite
现在你会发现有一个名为“mysite”的目录。
**步骤2:**
教程中指出:
要创建你的应用程序,请确保你在manage.py所在的同一目录中,然后输入以下命令:
该目录就是刚刚创建的,因此进入该目录:
cd mysite
如果你输入ls,那么你会在这个目录下看到:
- 一个名为manage.py的文件
- 令人困惑的是,另一个名为mysite的目录
第三步:
现在教程告诉你创建你的Django应用程序:
python manage.py startapp polls
现在,ls命令会显示这些文件:
manage.py
mysite
polls
可能会产生混淆,因为所有这些文件都在mysite目录下。
步骤4:
如果您想使用自定义用户模型,则官方建议在进行任何迁移之前立即开始项目。
好的,现在编辑mysite/settings.py文件,并添加以下行:
AUTH_USER_MODEL = 'polls.User'
然后编辑polls/models并添加以下内容:
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
**步骤5:**现在进行第一次迁移就可以了,是吗?
python manage.py makemigrations
但 SPLAT!
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项目时,你将更清楚地区分它们,并且不会再因为存在多个同名目录而感到困惑。
models.py
)中有一个from django.contrib.auth import forms
,其中调用了get_user_model()
。通过删除这些导入语句,问题得到解决。 - AlirezaAsadi