我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。

通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。

调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。

如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?


当前回答

你应该将polls.apps.PollsConfig添加到setting.py中的INSTALLED_APPS中

其他回答

django在执行makemigrations命令时没有检测到要迁移的内容有多种可能的原因。

migration folder You need a migrations package in your app. INSTALLED_APPS You need your app to be specified in the INSTALLED_APPS .dict Verbosity start by running makemigrations -v 3 for verbosity. This might shed some light on the problem. Full path In INSTALLED_APPS it is recommended to specify the full module app config path 'apply.apps.MyAppConfig' --settings you might want to make sure the correct settings file is set: manage.py makemigrations --settings mysite.settings specify app name explicitly put the app name in manage.py makemigrations myapp - that narrows down the migrations for the app alone and helps you isolate the problem. model meta check you have the right app_label in your model meta Debug django debug django core script. makemigrations command is pretty much straight forward. Here's how to do it in pycharm. change your script definition accordingly (ex: makemigrations --traceback myapp)

多个数据库:

当使用django的Db Router时,Router类(你的自定义Router类)需要实现allow_syncdb方法。

Makemigrations总是为模型更改创建迁移,但是如果 allow_migrate()返回False,

要为应用程序创建初始迁移,请运行makemigrations并指定应用程序名称。迁移文件夹将被创建。

./manage.py makemigrations <myapp>

你的应用必须首先包含在INSTALLED_APPS中(在settings.py内部)。

我知道这是一个老问题,但我一整天都在和这个问题斗争,我的解决方法很简单。

我的目录结构是这样的……

apps/
   app/
      __init__.py
      app_sub1/
           __init__.py
           models.py
      app_sub2/
           __init__.py
           models.py
      app_sub3/
           __init__.py
           models.py
   app2/
      __init__.py
      app2_sub1/
           __init__.py
           models.py
      app2_sub2/
           __init__.py
           models.py
      app2_sub3/
           __init__.py
           models.py
    main_app/
      __init__.py
      models.py

因为所有其他模型,直到我有一个问题,被导入到其他地方,最终从main_app导入,这是在INSTALLED_APPS注册的,我只是很幸运,他们都工作。

但是,由于我只将每个应用程序添加到INSTALLED_APPS,而不是app_sub*,当我最终添加一个新的模型文件时,没有导入到其他任何地方,Django完全忽略了它。

我的修复是添加一个models.py文件到每个应用程序的基本目录,就像这样…

apps/
   app/
      __init__.py
      models.py <<<<<<<<<<--------------------------
      app_sub1/
           __init__.py
           models.py
      app_sub2/
           __init__.py
           models.py
      app_sub3/
           __init__.py
           models.py
   app2/
      __init__.py
      models.py <<<<<<<<<<--------------------------
      app2_sub1/
           __init__.py
           models.py
      app2_sub2/
           __init__.py
           models.py
      app2_sub3/
           __init__.py
           models.py
    main_app/
      __init__.py
      models.py

然后从apps。app中添加。App_sub1 import *,以此类推到每个应用级models.py文件。

在那里……我花了很长时间才弄明白,我在任何地方都找不到解决方案……我甚至翻到了谷歌结果的第二页。

希望这能帮助到一些人!

在我的情况下,我忘记插入类参数

错误的:

class AccountInformation():

正确的

class AccountInformation(models.Model):

你应该将polls.apps.PollsConfig添加到setting.py中的INSTALLED_APPS中