如何使用 Django 中的 South 将数据从一种模型迁移到另一种模型?

发布于 2024-08-13 06:22:58 字数 219 浏览 3 评论 0原文

我创建了一个 Django 应用程序,它有自己的内部投票系统和一个名为 Vote 的模型来跟踪它。我想将投票系统重构为自己的应用程序,以便我可以重用它。但是,原始应用程序正在生产中,我需要创建一个数据迁移,它将获取所有投票并将它们移植到单独的应用程序中。

如何让两个应用程序参与迁移,以便我可以访问它们的两个模型?不幸的是,原始应用程序和单独的应用程序现在都有一个名为“投票”的模型,因此我需要注意任何冲突。

I created a Django app that had its own internal voting system and a model called Vote to track it. I want to refactor the voting system into its own app so I can reuse it. However, the original app is in production and I need to create a data migration that will take all the Votes and transplant them into the separate app.

How can I get two apps to participate in a migration so that I have access to both their models? Unfortunately, the original and separate apps both have a model named Vote now, so I need to be aware of any conflicts.

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

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

发布评论

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

评论(1

黎歌 2024-08-20 06:22:58

您是否尝试过db.rename_table

我首先会在新的或旧的应用程序中创建一个迁移,如下所示。

class Migration:

    def forwards(self, orm):
        db.rename_table('old_vote', 'new_vote')    

    def backwards(self, orm):
        db.rename_table('new_vote', 'old_vote')

如果这不起作用,您可以按照以下方式迁移循环中的每个项目:

def forwards(self, orm):
    for old in orm['old.vote'].objects.all():
        # create a new.Vote with old's data
models = {
    'old.vote' = { ... },
    'new.vote' = { ... },
}

注意:您必须使用 orm[...] 访问当前正在迁移的应用程序外部的任何模型。否则,标准 orm.Vote.objects.all() 表示法有效。

Have you tried db.rename_table?

I would start by creating a migration in either the new or old app that looks something like this.

class Migration:

    def forwards(self, orm):
        db.rename_table('old_vote', 'new_vote')    

    def backwards(self, orm):
        db.rename_table('new_vote', 'old_vote')

If that does not work you can migrate each item in a loop with something along these lines:

def forwards(self, orm):
    for old in orm['old.vote'].objects.all():
        # create a new.Vote with old's data
models = {
    'old.vote' = { ... },
    'new.vote' = { ... },
}

Note: You must use orm[...] to access any models outside the app currently being migrated. Otherwise, standard orm.Vote.objects.all() notation works.

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