jTiKey
jTiKey

Reputation: 743

Django CharField, choices and migration

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

Upvotes: 4

Views: 1679

Answers (2)

alTus
alTus

Reputation: 2215

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.

Upvotes: 6

vankovsky
vankovsky

Reputation: 94

Your default type doesn't match with your choices. Try to change to default='1'.

Upvotes: 1

Related Questions