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

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

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

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


当前回答

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

将你的应用添加到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教程。

其他回答

当向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
  │   └── ...

我这样解决了这个问题:

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".

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

./manage.py makemigrations <myapp>

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

操作方法:

1 .

确保你的应用必须包含在settings.py中的INSTALLED_APPS中

零食 : 2

python manage.py makemigrations <appname>

如果相同的消息显示(未检测到更改)

这对你的项目来说是非常危险的,所以在应用这个方法之前,请确保你的项目有备份。

方法2

重命名您的应用程序名称,并使用:

django-admin startapp <appname>

复制除旧应用程序之外的所有.py文件

迁移文件夹 pycache文件夹 init.py Test.py文件,如果您没有在其中编写代码

并粘贴到你最近制作的新应用程序中

记住,你必须为新应用程序创建完全相同的名称,否则你必须在项目中进行更多更改。

在创建一个名为deals的新应用程序时,我遇到了一个不同的问题。我想在应用程序中分离模型,所以我有两个模型文件,分别命名为deals.py和dealers.py。 当运行python manage.py makemigrations时,我得到:未检测到变化。

我继续,在__init__.py中,它位于与我的模型文件(deals和dealer)相同的目录中

from .deals import *
from .dealers import *

然后makemigrations命令起作用了。

事实证明,如果你没有在任何地方导入模型,或者你的模型文件名不是models.py,那么模型就不会被检测到。

发生在我身上的另一个问题是我在settings.py中编写应用程序的方式:

我有:

apps.deals

它应该包括根项目文件夹:

cars.apps.deals