Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django CharField, choices and migration

Tags:

django

For some reason Django asks me to migrate randomly, when this field isn't changed. The migration files are the same.

Model:

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)

Migrations: 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,
        ),
    ]
like image 442
jTiKey Avatar asked Oct 23 '25 13:10

jTiKey


1 Answers

It happened because PROGRESS_CHOICE is a dict while it must be a list or better a 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's choices accept any iterable so it worked but dict does not preserve order so every time you make migrations it is randomly shuffled while being transformed to list.

like image 88
alTus Avatar answered Oct 27 '25 01:10

alTus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!