我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
当前回答
对于这个错误,我的问题在于我包含了:
class Meta:
abstract = True
里面的模型,我想创建迁移。
其他回答
当向django api应用程序添加新模型并运行python manage.py makemigrations时,工具没有检测到任何新模型。
奇怪的是,旧模型确实被makemigrations选中了,但这是因为它们在urlpatterns链中被引用,而工具以某种方式检测到了它们。所以要注意这种行为。
这个问题是因为与models包对应的目录结构有子包,并且所有__init__.py文件都是空的。它们必须显式地在每个子文件夹和__init__.py模型中导入所有必需的类,以便Django使用makemigrationations工具来获取它们。
models
├── __init__.py <--- empty
├── patient
│ ├── __init__.py <--- empty
│ ├── breed.py
│ └── ...
├── timeline
│ ├── __init__.py <-- empty
│ ├── event.py
│ └── ...
这可以通过下面提到的两个步骤来完成。
将你的应用添加到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教程。
我这样解决了这个问题:
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".
另一个可能的原因是,如果您在另一个文件中定义了一些模型(而不是在包中),并且没有在其他任何地方引用它。
对我来说,简单地从.graph_model import *添加到admin.py(其中graph_model.py是新文件)就可以解决这个问题。
我读过很多关于这个问题的答案,通常都是简单地用其他方式进行移民。但对我来说,问题在于模型的Meta子类。
我有一个应用程序配置,说label = <应用程序名称>(在apps.py文件,旁边的models.py, views.py等)。如果你的元类没有与应用标签相同的标签(例如,因为你把一个太大的应用拆分成多个),就不会检测到任何变化(也不会有任何有用的错误消息)。所以在我的模型类中,我现在有:
class ModelClassName(models.Model):
class Meta:
app_label = '<app name>' # <-- this label was wrong before.
field_name = models.FloatField()
...
运行Django 1.10。