Django 1.8.4 makemigrations 创建模型 改变字段的顺序
Django 1.8.4 makemigrations 创建模型 改变字段的顺序
我们刚刚从1.6切换到了Django 1.8.4(所以第一次使用迁移),并且我们注意到在使用makemigrations
命令时出现了一个问题。当创建一个包含外键的新模型时,该命令会生成一个重新排序的迁移文件:它将所有的外键设置为最后,并按字母顺序重新组织它们。
这是一个例子:
class AnotherRandomModel(models.Model): attr1 = models.FloatField() class AnotherRandomModel2(models.Model): attr1 = models.FloatField() class RandomModel(models.Model): fk2 = models.ForeignKey(AnotherRandomModel2) attr2 = models.FloatField() fk1 = models.ForeignKey(AnotherRandomModel) attr1 = models.FloatField()
这将生成以下迁移文件:
class Migration(migrations.Migration): dependencies = [] operations = [ migrations.CreateModel( name='AnotherRandomModel', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('attr1', models.FloatField()), ], ), migrations.CreateModel( name='AnotherRandomModel2', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('attr1', models.FloatField()), ], ), migrations.CreateModel( name='RandomModel', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('attr2', models.FloatField()), ('attr1', models.FloatField()), ('fk1', models.ForeignKey(to='inventorylab.AnotherRandomModel')), ('fk2', models.ForeignKey(to='inventorylab.AnotherRandomModel2')), ], ), ]
你可以看到它保留了非外键字段的顺序,但将两个外键设置为最后并重新排序了它们。
不同的是,在模型和数据库上没有相同的顺序,这确实令人困扰。是否有人知道如何强制该命令保持模型的顺序?
我知道我可以手动编辑创建的迁移文件,但我想避免这样做。