Django可以为现有表中的每一行在新表中创建默认行吗?

问题描述 投票:0回答:1

我有一个填充有数据的公司表。引入分支表的需求已经出现。在Django中将使用哪种机制来使用company表中的现有数据来为每个现有company条目创建一个基本分支条目。

“公司A将在迁移过程中获得默认的总部分支。”

是否有一种方法可以将功能绑定到迁移,或者我需要创建一个自定义迁移来在处理表创建过程之后执行该操作?

models.py

class Company(models.Model):
    name = models.CharField(max_length=64)

# This is the new model/table
# Each Company must have at least one branch post migration
class Branch(models.Model):
    branch_id = models.IntegerField(
        verbose_name="Branch ID",
        null=False,
        default=1000),
    parent_branch = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, blank=False, null=False)
python django django-migrations
1个回答
0
投票

Found the answer here.将其作为两个单独的操作执行,在此操作中我创建了新表而不更改旧表,执行了数据迁移,然后部署了从其运行的新代码。

© www.soinside.com 2019 - 2024. All rights reserved.