简体   繁体   English

从一个Django模型迁移到使用外键引用的两个模型

[英]Migrate from one django model to two models referenced with a foreign key

I need to outsource some of the attribues in the following Django model: 我需要将以下Django模型中的某些属性外包:

class TextResult(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    text = models.ForeignKey(Text)
    # following fields will be in the referenced model
    wpm = models.FloatField(default=0.0)
    accuracy = models.FloatField(default=1.0,
                                 validators=[MinValueValidator(0.0),
                                             MaxValueValidator(1.0)])

to a model, that references to the specific data: 模型,该模型引用特定数据:

class TextResult(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    text = models.ForeignKey(Text)
    typing_result = models.ForeignKey(TypingResult)

class TypingResult(models.Model):
    wpm = models.FloatField(default=0.0)
    accuracy = models.FloatField(default=1.0,
                                 validators=[MinValueValidator(0.0),
                                             MaxValueValidator(1.0)])

The problem is, that there is already some data in the database, so I have to migrate the data to the new structure, what is the easiest, cleanest way to achieve that? 问题是数据库中已经有一些数据,因此我必须将数据迁移到新结构中,最简单,最干净的方法是什么?

I would do a 3-step migration. 我将进行三步迁移。

  1. Create the new fields 创建新字段

1.1. 1.1。 Create TypingResult model and the typing_result = models.ForeignKey(TypingResult, blank=True, null=True) field. 创建TypingResult模型,然后创建TypingResult typing_result = models.ForeignKey(TypingResult, blank=True, null=True)字段。 Make sure the FK is optional by making it blank-able and null-able 通过将FK设置为​​空白和可空来确保FK是可选的

1.2 Checkpoint by migrating 1.2迁移检查点

  1. Data Migration 数据迁移

2.1 Create an empty migration using this guide https://docs.djangoproject.com/en/1.11/topics/migrations/#data-migrations and add instructions for data migrations. 2.1使用本指南https://docs.djangoproject.com/zh-CN/1.11/topics/migrations/#data-migrations创建一个空迁移,并添加有关数据迁移的说明。

2.2 The data migration steps are as follows: 2.2数据迁移步骤如下:

  • Iterate through all TextResult for each of them create a TypingResult with the corresponding data 遍历所有TextResult ,为它们每个创建一个带有相应数据的TypingResult

  • Link the TypingResult to the TextResult through FK 通过FK将TextResult链接到TypingResult

2.3 Run the migration to checkpoint 2.3运行迁移到检查点

  1. Cleanup 清理

3.1 Delete the wpm and accuracy fields on the TextResult and make the ForeignKey non-optional. 3.1删除TextResult上的wpm和TextResult字段,并使ForeignKey为非可选。

3.2 Run the migration 3.2运行迁移

Conclusion 结论

This can probably all be done in 1 step, but it's best to understand what is going on. 这大概可以一步完成,但是最好了解发生了什么。 Also adding pdb before a .save() call will allow you to inspect the steps for the data migration 此外,在.save()调用之前添加pdb将使您能够检查数据迁移的步骤

import pdb; pdb.set_trace()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM