如何使用 Django 中的 South 将数据从一种模型迁移到另一种模型?
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过db.rename_table?
我首先会在新的或旧的应用程序中创建一个迁移,如下所示。
如果这不起作用,您可以按照以下方式迁移循环中的每个项目:
注意:您必须使用
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.
If that does not work you can migrate each item in a loop with something along these lines:
Note: You must use
orm[...]
to access any models outside the app currently being migrated. Otherwise, standardorm.Vote.objects.all()
notation works.