Django CharField,选择和迁移

时间:2015-06-03 20:33:41

标签: django

出于某种原因,当这个字段没有改变时,Django要求我随机迁移。迁移文件是相同的。

型号:

PROGRESS_CHOICE = {
        ('1', '1.start'),
        ('2', '2.driver_arrived_pick_up'),
        ('3', '3.hope_in'),
        ('4', '4.driver_arrived_destination'),
        ('5', '5.end')
    }

    progress = models.CharField(max_length=20, choices=PROGRESS_CHOICE, default=1, blank=True)

迁移:1。

class Migration(migrations.Migration):
    dependencies = [
        ('api', '0031_auto_20150603_1515'),
    ]

    operations = [
        migrations.AlterField(
            model_name='ride',
            name='progress',
            field=models.CharField(max_length=20, default=1, blank=True, choices=[('1', '1.start'), ('3', '3.hope_in'), ('4', '4.driver_arrived_destination'), ('2', '2.driver_arrived_pick_up'), ('5', '5.end')]),
            preserve_default=True,
        ),
    ]

2

class Migration(migrations.Migration):
    dependencies = [
        ('api', '0032_auto_20150603_1734'),
    ]

    operations = [
        migrations.AlterField(
            model_name='ride',
            name='progress',
            field=models.CharField(default=1, max_length=20, choices=[('1', '1.start'), ('4', '4.driver_arrived_destination'), ('5', '5.end'), ('3', '3.hope_in'), ('2', '2.driver_arrived_pick_up')], blank=True),
            preserve_default=True,
        ),
    ]

2 个答案:

答案 0 :(得分:2)

之所以发生这种情况,是因为PROGRESS_CHOICEdict,而list或更好tuple

PROGRESS_CHOICES = (
    ('1', '1.start'),
    ('2', '2.driver_arrived_pick_up'),
    ('3', '3.hope_in'),
    ('4', '4.driver_arrived_destination'),
    ('5', '5.end'),
)

CharField' choices接受任何迭代,因此它有效,但dict不保留顺序,所以每次进行迁移时,它都会在转换为列表时随机洗牌。

答案 1 :(得分:1)

您的默认类型与您的选择不匹配。尝试更改为默认=' 1'。