【问题标题】:How to split a model field's value into the multiple model fields in django migration?如何在 django 迁移中将模型字段的值拆分为多个模型字段?
【发布时间】:2023-04-03 03:30:01
【问题描述】:

我想写一个新的迁移文件来把旧的坏数据改成更好的。例如,旧模型是这样的:

from django.db import models

class Person(models.Model):
    fullname = models.CharField(max_length=250, null=True)
    information = models.CharField(max_length=350, null=True)

fullname 的值是这样的:

first_name:George;last_name:Adam Pince Green

first_namelast_name 的顺序始终相同。 信息的价值是这样的:

id_code:0021678913;born_in:Canada;birth_year:1975

birth_year:1990;born_in:Portugal;id_code:0219206431

未排序。 现在我需要编写一个 migration 文件,将这个 fullnameinformation 值拆分为新模型,例如:

from django.db import models


class Person(models.Model):
    first_name = models.CharField(max_length=30, null=True)
    last_name = models.CharField(max_length=50, null=True)
    id_code = models.CharField(max_length=10, null=True)
    born_in = models.CharField(max_length=30, null=True)
    birth_year = models.PositiveSmallIntegerField(null=True)

【问题讨论】:

    标签: django django-migrations


    【解决方案1】:

    对于您的代码,您可以编写如下内容:

    from django.db import migrations, models
    
    def ammend_the_data(apps, _):
        Person = apps.get_model('people', 'Person')
        people = Person.objects.all().iterator()
    
        for person in people:
            fullnamesplit = person.fullname.split(';')
            first_name = fullnamesplit[0].split(':')[1]
            last_name = fullnamesplit[1].split(':')[1]
    
            informationsplit = person.information.split(';')
            info2 = {}
            for info in informationsplit:
                info3 = info.split(':')
                info2.update({info3[0] : info3[1]})
            
            person.first_name = first_name
            person.last_name = last_name
            person.id_code = info2.get('id_code')
            person.born_in = info2.get('born_in')
            person.birth_year = int(info2.get('birth_year'))
    
            person.save()
    
    
    
    class Migration(migrations.Migration):
        initial = True
    
        dependencies = [
        ]
    
        operations = [
            migrations.CreateModel(
                name='Person',
                fields=[
                    ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                    ('fullname', models.CharField(max_length=250, null=True)),
                    ('information', models.CharField(max_length=350, null=True)),
                    
                    ('first_name', models.CharField(max_length=30, null=True)),
                    ('last_name', models.CharField(max_length=50, null=True)),
                    ('id_code', models.CharField(max_length=10, null=True)),
                    ('born_in', models.CharField(max_length=30, null=True)),
                    ('birth_year', models.PositiveSmallIntegerField(null=True)),
                ],
            ), 
            
            migrations.RunPython(
                code= ammend_the_data)
        ]
    

    正如您所看到的名字和姓氏,我们可以使用索引来查找正确的字符串。但是为了拆分信息,我们必须使用字典,因为我们不知道输入数据的顺序。但显然你也可以在第一部分使用字典。

    重要的部分是person.save() 行。您不必以任何不同的方式对待 Django CharFields。您可以将它们视为字符串。完成更改后别忘了致电model_instance.save()

    您也可以看到,最好使用 QuerySet Iterator 来遍历数据点,因为如果您不使用它,遍历大数据集将是一个问题。 有关何时使用iterator() 的更多信息,请参阅此处:iterator()

    【讨论】:

      【解决方案2】:

      您可以更改新字段,然后使用django.db.migrations.operations.RunPython 从旧字段中提取值并保存到新字段。最后删除旧字段。 https://docs.djangoproject.com/en/3.1/ref/migration-operations/#django.db.migrations.operations.RunPython

      【讨论】:

      • 感谢您的回答。但我的主要问题是如何拆分旧字段的值并将它们分配给新模型字段
      猜你喜欢
      • 2019-12-08
      • 2021-12-15
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2019-09-29
      • 1970-01-01
      相关资源
      最近更新 更多