Django迁移时如何设置/提供默认值? [英] How can I set/provide a default value while django migration?
问题描述
场景:
我有一个模型,客户
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.CharField(max_length=100)
现在我使用 ForeignKey
关系更新了 company
属性,如下所示,
and now I updated the company
attribute witha ForeignKey
relationship as below,
class Company(models.Model):
name = models.CharField(max_length=100)
location = models.CharField(max_length=100)
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.ForeignKey(Company)
我需要的是,将新迁移应用于数据库时,相应的 Company
实例必须自动生成并映射到< code >客户实例。
可能吗?我该如何实现呢?
What I need is, when the new migrations applied to the DB,corresponding Company
instance must automatically generate and map to the company
attribute of Customer
instance.
Is that possible? How can I achieve this ?
推荐答案
让我们从您的原始模型开始,然后逐步进行。
Let's start from your original model and do it step by step.
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.CharField(max_length=100)
首先,您必须保留原始字段并创建一个新字段,以便以后可以还原旧数据。
First you would have to keep the original field and create a new one, to be able to restore the old data afterwards.
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.CharField(max_length=100)
_company = models.ForeignKey(Company)
现在您可以使用 manage.py makemigrations <创建第一次迁移/ code>。然后,您将必须创建数据迁移。使用
manage.py makemigrations yourapp --empty
创建迁移并更新生成的文件:
Now you can create a first migration with manage.py makemigrations
. Then you will have to create a data migration. Create the migration using manage.py makemigrations yourapp --empty
and update the generated file:
from django.db import migrations
def export_customer_company(apps, schema_editor):
Customer = apps.get_model('yourapp', 'Customer')
Company = apps.get_model('yourapp', 'Company')
for customer in Customer.objects.all():
customer._company = Company.objects.get_or_create(name=customer.company)[0]
customer.save()
def revert_export_customer_company(apps, schema_editor):
Customer = apps.get_model('yourapp', 'Customer')
Company = apps.get_model('yourapp', 'Company')
for customer in Customer.objects.filter(_company__isnull=False):
customer.company = customer._company.name
customer.save()
class Migration(migrations.Migration):
dependencies = [
('yourapp', 'xxxx_previous_migration'), # Note this is auto-generated by django
]
operations = [
migrations.RunPython(export_customer_company, revert_export_customer_company),
]
以上迁移将填充您的公司
模型和客户。 _company
字段,根据 Customer.company
。
The above migration will populate your Company
model and Customer._company
field according to Customer.company
.
现在您可以删除旧的 Customer.company
并重命名 Customer._company
。
Now you can drop the old Customer.company
and rename Customer._company
.
class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
company = models.ForeignKey(Company)
最终 manage.py makemigrations
和 manage.py迁移
。
这篇关于Django迁移时如何设置/提供默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!