Django 4.2.1与django-cookiecutter下makemigrations不生成新模型迁移文件
Django makemigrations无法生成新模型迁移文件的问题排查
环境信息
- 系统:MacOS Ventura 13.3.1
- Django版本:4.2.1
- django-cookiecutter版本:2023.05.09
问题重现
- 在目标应用的
models.py中添加新模型:
from django.db import models class TestModel(models.Model): name = models.CharField(max_length=50)
- 执行命令为应用创建空迁移文件:
python manage.py makemigrations myapp --empty
- 再次执行
makemigrations命令,未生成对应TestModel的迁移文件
已完成的排查动作
- 确认
TestModel所属应用已加入settings.py的INSTALLED_APPS列表 models.py无语法错误,执行python manage.py check未发现问题- 使用
--verbosity 3参数执行makemigrations,无相关错误或警告输出 - 应用的
migrations目录中已存在初始迁移文件0001_initial.py
项目目录结构
└─ base ├─ .dockerignore ├─ .editorconfig ├─ .envs │ ├─ .local │ │ ├─ .django │ │ └─ .postgres │ └─ .production │ ├─ .django │ └─ .postgres ├─ .pre-commit-config.yaml ├─ .readthedocs.yml ├─ CONTRIBUTORS.txt ├─ LICENSE ├─ MailHog ├─ README.md ├─ SECURITY.md ├─ base │ ├─ __init__.py │ ├─ conftest.py │ ├─ contrib │ │ ├─ __init__.py │ │ └─ sites │ │ ├─ __init__.py │ │ └─ migrations │ │ ├─ 0001_initial.py │ │ ├─ 0002_alter_domain_unique.py │ │ ├─ 0003_set_site_domain_and_name.py │ │ ├─ 0004_alter_options_ordering_domain.py │ │ ├─ __init__.py │ ├─ media │ ├─ static │ │ ├─ css │ │ ├─ fonts │ │ ├─ images │ │ ├─ js │ │ ├─ sass │ │ └─ webpack_bundles │ ├─ templates │ │ ├─ 403.html │ │ ├─ 404.html │ │ ├─ 500.html │ │ ├─ account │ │ │ ├─ account_inactive.html │ │ │ ├─ base.html │ │ │ ├─ email.html │ │ │ ├─ email_confirm.html │ │ │ ├─ login.html │ │ │ ├─ logout.html │ │ │ ├─ password_change.html │ │ │ ├─ password_reset.html │ │ │ ├─ password_reset_done.html │ │ │ ├─ password_reset_from_key.html │ │ │ ├─ password_reset_from_key_done.html │ │ │ ├─ password_set.html │ │ │ ├─ signup.html │ │ │ ├─ signup_closed.html │ │ │ ├─ verification_sent.html │ │ │ └─ verified_email_required.html │ │ ├─ base.html │ │ ├─ pages │ │ │ ├─ about.html │ │ │ └─ home.html │ │ └─ users │ │ ├─ user_detail.html │ │ └─ user_form.html │ ├─ users │ │ ├─ __init__.py │ │ ├─ adapters.py │ │ ├─ admin.py │ │ ├─ api │ │ ├─ apps.py │ │ ├─ context_processors.py │ │ ├─ forms.py │ │ ├─ managers.py │ │ ├─ migrations │ │ │ ├─ 0001_initial.py │ │ │ ├─ __init__.py │ │ ├─ models.py │ │ ├─ tasks.py │ │ ├─ urls.py │ │ └─ views.py │ ├─ utils │ │ ├─ __init__.py │ │ └─ storages.py │ └─ website │ ├─ __init__.py │ ├─ __pycache__ │ ├─ admin.py │ ├─ apps.py │ ├─ migrations │ │ ├─ 0001_initial.py │ │ ├─ __init__.py │ ├─ models │ │ ├─ __init__.py │ ├─ models.py │ ├─ tests.py │ ├─ urls.py │ └─ views.py ├─ config │ ├─ __init__.py │ ├─ api_router.py │ ├─ asgi.py │ ├─ celery_app.py │ ├─ settings │ │ ├─ __init__.py │ │ ├─ base.py │ │ ├─ local.py │ │ ├─ production.py │ │ └─ test.py │ ├─ urls.py │ ├─ websocket.py │ └─ wsgi.py ├─ docs ├─ dump.rdb ├─ local.yml ├─ locale │ └─ README.rst ├─ manage.py ├─ merge_production_dotenvs_in_dotenv.py ├─ package-lock.json ├─ package.json ├─ production.yml ├─ pyproject.toml ├─ requirements ├─ setup.cfg ├─ staticfiles ├─ tests ├─ webpack └─ webpack-stats.json
可能原因及额外排查步骤
1. 空迁移文件干扰模型状态检测
执行makemigrations --empty会生成一个空迁移,Django会将其视为已覆盖当前模型状态。解决方式:
- 删除该空迁移文件,重新执行
python manage.py makemigrations myapp - 或手动在空迁移中添加创建
TestModel的代码,再执行migrate
2. 应用模型模块结构问题
从目录结构看,website应用下有models子目录,需确认:
TestModel是否被导入到website/models/__init__.py中?Django默认检测应用根目录的models.py,若存在models子目录,需确保模型被正确导入,否则无法识别- 检查
website/apps.py中的name属性是否与INSTALLED_APPS中注册的应用名一致
3. 迁移缓存异常
Django会缓存迁移状态,可能存在缓存未更新:
- 删除应用
migrations目录下的__pycache__文件夹 - 执行
python manage.py clearcache清理Django缓存(Django 4.2+支持) - 检查数据库
django_migrations表,确认该应用的迁移记录是否正确,有无异常条目
4. 模型Meta配置问题
检查TestModel的Meta选项:
- 是否设置了
managed = False?若为False,Django不会为该模型生成迁移 - 是否存在自定义元类或非默认的模型基类,导致迁移检测逻辑异常
5. 多环境配置加载错误
cookiecutter生成的项目有多环境配置,需确认:
- 当前使用的settings配置是否正确加载了
INSTALLED_APPS列表 - 执行命令时是否指定了正确的settings文件,例如:
python manage.py makemigrations myapp --settings=config.settings.local
6. 应用命名冲突或路径错误
- 检查
INSTALLED_APPS中是否有重名应用 - 确认应用注册的路径是否正确,例如是否应为
base.website而非website
内容的提问来源于stack exchange,提问作者RosenbladCurt
相关产品推荐
相关产品推荐

