[Answered ]-How to update data in Database when status_choice removed from models

1👍

Add a data migration operation to the migration that changes the choices, update the values for the old rows there

from django.db import migrations, models


def update_status(apps, schema_editor):
    MyModel = apps.get_model('yourappname', 'MyModel')
    MyModel.objects.filter(status__in=['FIELD_4', 'FIELD_5']).update(status='FIELD_1')


class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', 'xxx_previous'),
    ]

    operations = [
        migrations.RunPython(update_status),
        migrations.AlterField(
            model_name='mymodel',
            name='status',
            field=models.CharField(choices=[('FIELD_1', 'value FIELD_1'), ('FIELD_2', 'value FIELD_2'), ('FIELD_3', 'value FIELD_3')], default='FIELD_1', max_length=50),
        ),
    ]

Leave a comment