当我们在 Django 中将模型 Charfield 更改为 IntegerField 时,现有数据会发生什么?

what happens to existing data when we change model Charfield to IntegerField in django?

应用以下迁移时,

class Migration(migrations.Migration):
    dependencies = [
        ('outlets', '0009_auto_20190920_1155'),
    ]

    operations = [
        migrations.AlterField(
            model_name='outlet',
            name='country',
            field=models.IntegerField(choices=[(1, 'UAE'), (2, 'India')],
                                      verbose_name='Country'),
        ),
    ]

出现以下错误,

return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for integer: "United States"

这是因为我正在将 CharField 更改为 IntegerField 并且以下数据 "United States" 已经存在于数据库中,另外我在类似的问题中读到,django 迁移无法处理此类更改。

有什么方法可以在不删除现有数据的情况下执行此操作?

您需要在三个单独的迁移中执行此操作。

首先,将 IntegerField 添加为 null=True 或默认值 0。

其次,添加一个数据迁移,在现有的 CharField 中获取值,查找匹配的整数值,并将其设置为新的 IntegerField。

第三,删除 CharField,重命名 IntegerField 并删除 default/null。

如果你想让事情顺利进行,你需要在迁移中集成数据突变

作为示例:

from django.db import migrations, models
import django.db.models.deletion

def migrate_country(apps, schema_editor):
    Outlet = apps.get_model('your_app_name', 'Outlet')
    # You can either do
    Outlet.objects.all().update(
        temp_country= 'what_ever_update you want'
    )
    # Or
    for outlet in Outlet.objects.all():
        outlet.temp_country='your_transformation_result'
        outlet.save() # Please note django will use it's save method. 
                      # Your eventual override will just be ignore

def reverse_country(apps, schema_editor):
    """
    Do want you want when rolling back your migration
    """
    ....

class Migration(migrations.Migration):

    dependencies = [
        ...
    ]

    operations = [
        migrations.AddField(
            model_name='Outlet',
            name='temp_outlet',
            name='country',
            field=models.IntegerField(choices=[(1, 'UAE'), (2, 'India')],
                                      verbose_name='Country'),
        ),
        # Now, you have temp_outlet available on your schema!
        migrations.RunPython(migrate_country, reverse_country),
        # We dump "old" field
        migrations.RemoveField(
            model_name='article',
            name='outlet',
        ),
        # We rename the 'temp' field as excpected!
        migrations.RenameField(
        model_name='Outlet',
            old_name='oldname',
            new_name='newname',
    ),
    ]