我在使用Django REST Framework时遇到了一个问题
在PyCharm中执行makemigrations和 migrate时会发生这种情况
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL指向尚未安装的模型 'app.User'
解决方案
This error means Django can't find the User model in the app referenced by AUTH_USER_MODEL. It's almost always one of these causes:
这个错误意味着Django找不到在 AUTH_USER_MODEL 引用的应用中的 User 模型。几乎总是由以下原因之一引起:
1. The app isn't in INSTALLED_APPS
1.该应用不在 INSTALLED_APPS 中
Check your settings.py:
检查你的 settings.py:
INSTALLED_APPS = [
...
'app', # <-- must be listed here
]
INSTALLED_APPS = [
...
'app', # <-- must be listed here
]
If your app is nested, e.g. myproject/app, or has an AppConfig, you may need the full dotted path instead, like 'app.apps.AppConfig' or 'myproject.app'.
如果你的应用是嵌套的,例如 myproject/app,或有一个 AppConfig,你可能需要完整的点分路径,例如 'app.apps.AppConfig' 或 'myproject.app'。
2. AUTH_USER_MODEL doesn't match the app's actual label
2. AUTH_USER_MODEL 与应用的实际标签不匹配
AUTH_USER_MODEL = 'app.User'
The part before the dot must be the app's label (usually the app folder name, or whatever's set as label in the app's AppConfig), not the project name or an arbitrary string. Check app/apps.py:
点号前面的部分必须是应用的标签(通常是应用文件夹的名称,或在应用的 AppConfig 中设置为 label 的名称),而不是项目名或任意字符串。检查 app/apps.py:
class AppConfig(AppConfig):
name = 'app'
label = 'app' # this is what AUTH_USER_MODEL should reference
3. There's no User model actually defined in app/models.py
3.在 app/models.py 中实际上没有定义 User 模型
Make sure you have something like:
请确保你有类似这样的定义:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
4. Circular/ordering issue — if app depends on another app that isn't installed, or is misspelled (e.g. 'apps' vs 'app'), you'll get this same error.
4.循环/排序问题 — 如果 app 依赖的另一个应用未安装,或拼写错误(例如 'apps' 与 'app' 不一致),你也会看到同样的错误。
Quick checklist to fix:
快速排错清单:
- Run
python manage.py shelland tryfrom django.apps import apps; print(apps.get_app_configs())— confirm your app shows up with the label you expect. -
运行
python manage.py shell并尝试from django.apps import apps; print(apps.get_app_configs())— 确认你的应用以你期望的标签出现在列表中。 -
Double check spelling/case in both
INSTALLED_APPSandAUTH_USER_MODEL— they're case-sensitive and must match exactly. -
同时检查
INSTALLED_APPS和AUTH_USER_MODEL的拼写/大小写——它们对大小写敏感,必须完全一致。 -
If you just added a custom user model to an existing project, make sure you haven't already run migrations with the default
auth.User— that's a separate, messier problem requiring a fresh database or careful migration surgery. - 如果你刚在现有项目中添加了自定义用户模型,确保你还没有使用默认的
auth.User运行过迁移——这是一个独立且更棘手的问题,通常需要全新的数据库或谨慎的迁移操作。
If you paste your INSTALLED_APPS, AUTH_USER_MODEL line, and app/apps.py, we might be able to pinpoint the exact mismatch.
如果把 INSTALLED_APPS、AUTH_USER_MODEL 行,以及 app/apps.py 粘贴过来,我们也许能够准确定位出不匹配的具体位置。