南迁移默认值来自其他列?

时间:2021-01-08 20:47:05

I have a model that has one column right now, description, that populates an area on a few different pages of the site. The client wants this split up into a couple different columns, so they can populate different values in certain parts of the site. So I have to change that description column to frontpage_description and resourcepage_description. I am trying to come up with a way to do this in South so that the value of the description column is the (initial) default value for both of the "new" columns. This is what I have so far:

我有一个模型,现在有一个列,描述,填充网站的几个不同页面上的区域。客户希望将此拆分为几个不同的列,以便他们可以在站点的某些部分填充不同的值。所以我必须将该描述列更改为frontpage_description和resourcepage_description。我试图想出一种在南方执行此操作的方法,以便说明列的值是两个“新”列的(初始)默认值。这是我到目前为止:

# file: project/app/xxxx_mymigration.py
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models

class Migration(SchemaMigration):
    def forwards(self, orm):
        db.rename_column('myapp', 'description', 'frontpage_description')
        db.add_column('myapp', 'resourcepage_description', self.gf('myfields.TextField')(default='CHANGEME'), keep_default=False)

    def backwards(self, orm):
        db.rename_column('myapp', 'frontpage_description', 'description')
        db.delete_column('myapp', 'resourcepage_description')

    models = {
        # ...

The part I am wondering about is the self.gf(...)(default='CHANGEME'), I am wondering if there is a way to set the value of description or frontpage_description to be the default value for resourcepage_description?

我想知道的部分是self.gf(...)(默认='CHANGEME'),我想知道是否有办法将description或frontpage_description的值设置为resourcepage_description的默认值?

I have looked into the orm parameter, which does allow you to access models during the migration, but all the examples I have come across involve using it to define relations during a migration, and not actually accessing individual records.

我查看了orm参数,它允许您在迁移期间访问模型,但我遇到的所有示例都涉及使用它来定义迁移期间的关系,而不是实际访问单个记录。

Am I just going to have to split this up into a schemamigration and a datamigration?

我是否只需要将其拆分为模式迁移和数据迁移?

1 个解决方案

#1


2  

This is exactly a three-step: schema migration to add the columns, a data migration to set them, and another schema migration to delete the original description column. 90% of that is done for you from the ./manage command, so it's not as if this is tragically more work.

这只是一个三步骤:用于添加列的模式迁移,用于设置列的数据迁移,以及用于删除原始描述列的另一个模式迁移。其中90%是通过./manage命令为你完成的,所以这并不像是悲剧性的更多工作。

#1


2  

This is exactly a three-step: schema migration to add the columns, a data migration to set them, and another schema migration to delete the original description column. 90% of that is done for you from the ./manage command, so it's not as if this is tragically more work.

这只是一个三步骤:用于添加列的模式迁移,用于设置列的数据迁移,以及用于删除原始描述列的另一个模式迁移。其中90%是通过./manage命令为你完成的,所以这并不像是悲剧性的更多工作。