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

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

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

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


当前回答

可能的原因是删除了现有的db文件和migrations文件夹 你可以使用python manage.py makemigrations <app_name>这应该可以工作。我曾经遇到过类似的问题。

其他回答

我读过很多关于这个问题的答案,通常都是简单地用其他方式进行移民。但对我来说,问题在于模型的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。

解决方案是你必须把你的应用包含在INSTALLED_APPS中。

我错过了它,我发现了同样的问题。

在指定我的应用程序名称迁移成功

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'boards',
]

请注意,我在最后提到了boards,这是我的应用程序名称。

我这样解决了这个问题:

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

这可能会帮助其他人,因为我最终花了几个小时试图追踪它。

如果您的模型中有同名的函数,这将删除该值。事后看来很明显,但尽管如此。

所以,如果你有这样的东西:

class Foobar(models.Model):
    [...]
    something = models.BooleanField(default=False)

    [...]
    def something(self):
        return [some logic]

在这种情况下,该函数将覆盖上面的设置,使其“隐形”进行移民。

另一个会导致这种情况的是字段后面的尾随逗号,这将导致在makemigrationations期间跳过字段:

class MyModel(models.Model):
    name = models.CharField(max_length=64, null=True)  # works
    language_code = models.CharField(max_length=2, default='en')  # works
    is_dumb = models.BooleanField(default=False),  # doesn't work

我有一个拖尾,在一行中,可能来自复制粘贴。带有is_dumb的代码行不会使用./manage.py makemigrations创建模型迁移,因为Python认为它是一个元组,而Django不认为它是一个字段。