Django 1.8.4 makemigrations CreateModel changes field's order

坚强是说给别人听的谎言 提交于 2019-12-25 06:39:08

问题


We've just switched to Django 1.8.4 (from 1.6, so first time using migrations), and we've noticed an issue when using the makemigrations command. The issue happens when creating a new model that contains Foreign Keys. The command generates a migration file with the field order changed: it sets all the FKs last, and reorganizes them by alphabetical order.

Here's an example :

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()

That will generate this migration file :

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')),
            ],
        ),
    ]

You can see how it kept the non-FK fields order, but set both FK at the end an re-ordered them.

That's quite disturbing not to have the same order on the model as on the database. Does anyone know how to force the command to keep the order of the model?

I know I can always edit manually the created migration file, but I'll like to avoid doing that.

来源:https://stackoverflow.com/questions/32454421/django-1-8-4-makemigrations-createmodel-changes-fields-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!