小编Ing*_*upp的帖子

我可以将架构和数据(南)迁移合并为一个吗?

我想使用South 将字段的鸣喇叭和数据从一个模型移动到另一个模型:

class Foo(models.Model):
    foofield = models.CharField()
    honk = models.PositiveIntegerField()

class Bar(models.Model):
    barfield = models.CharField()
Run Code Online (Sandbox Code Playgroud)

我以前做过这个,使用3次单独的迁移:

  1. 模式迁移,将honk添加到Bar
  2. 数据迁移,将所有Foo.honk数据复制到Bar.honk
  3. 另一个模式迁移,从Foo中删除了honk

我可以在一次迁移中执行这三个步骤吗?

我已经了解到南方的模式和数据迁移之间没有太大差别,所以我想也许这样的东西可能会起作用(这就是上面的三个迁移只是一个):

class Migration(DataMigration):
    def forwards(self, orm):
        # add column
        db.add_column('myapp_bar', 'honk', self.gf('django.db.models.fields.PositiveIntegerField')(default='0'), keep_default=False)

        # copy data
        for foo in Foo.objects.all():
            # find the right bar here and then ...
            bar.honk = foo.honk
            bar.save()

        # remove old column
        db.delete_column('myapp_foo', 'honk')
Run Code Online (Sandbox Code Playgroud)

这会起作用还是会失败,因为我(南方冻结)的orm还不知道Bar.honk呢?或者我做错了,在一次迁移中有更好的方法来做这种事情吗?

django-south

14
推荐指数
1
解决办法
2247
查看次数

标签 统计

django-south ×1