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

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

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

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


当前回答

我这样解决了这个问题:

Erase the "db.sqlite3" file. The issue here is that your current data base will be erased, so you will have to remake it again. Inside the migrations folder of your edited app, erase the last updated file. Remember that the first created file is: "0001_initial.py". For example: I made a new class and register it by the "makemigrations" and "migrate" procedure, now a new file called "0002_auto_etc.py" was created; erase it. Go to the "pycache" folder (inside the migrations folder) and erase the file "0002_auto_etc.pyc". Finally, go to the console and use "python manage.py makemigrations" and "python manage.py migrate".

其他回答

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

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

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文件。

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

希望这能帮助到一些人!

我在django 3.0中遇到了类似的问题,根据官方文档中的迁移部分,运行这个足够更新我的表结构:

python manage.py makemigrations
python manage.py migrate

但是输出总是一样的:在我执行'makemigrations'脚本后,我的模型'未检测到变化'。 我在models.py的模型上有一个语法错误,我想在db上更新:

field_model : models.CharField(max_length=255, ...)

而不是:

field_model = models.CharField(max_length=255, ...)

解决了这个愚蠢的错误,有了这些命令,迁移就没有问题了。也许这能帮到别人。

对于这个错误,我的问题在于我包含了:

class Meta:
   abstract = True

里面的模型,我想创建迁移。

这可以通过下面提到的两个步骤来完成。

将你的应用添加到settings.py > INSTALLED_APPS 打开admin.py


from .models import upImg
# Register your models here.
admin.site.register(upImg)

注意:将upImg替换为models.py中定义的className

在此之后,查看是否仍然有python manage.py makemigrations。如果有,也执行python manage.py migrate。

更多信息请参考django教程。

我的问题(以及解决方案)与上面描述的不同。

我没有使用models.py文件,而是创建了一个models目录,并在那里创建了my_model.py文件,我把我的模型放在那里。Django找不到我的模型,所以它写道没有迁移可以应用。

我的解决方案是:在my_app/models/__init__.py文件中,我添加了这一行: 导入MyModel