其他列的南移默认值?

发布于 2024-12-02 16:11:33 字数 1233 浏览 1 评论 0原文

我有一个模型,现在有一个列,description,它填充网站的几个不同页面上的一个区域。客户希望将其分成几个不同的列,以便他们可以在网站的某些部分填充不同的值。因此,我必须将 description 列更改为 frontpage_descriptionresourcepage_description。我正在尝试想出一种方法在南部执行此操作,以便 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 = {
        # ...

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

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

我是否必须将其分为架构迁移和数据迁移?

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:

# 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?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风启觞 2024-12-09 16:11:33

这实际上是一个三步:用于添加列的架构迁移、用于设置列的数据迁移以及用于删除原始 description 列的另一个架构迁移。其中 90% 是通过 ./manage 命令为您完成的,因此这并不是可悲的更多工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文