将Django模型从一个应用程序移动到另一个应用程序 [英] Move Django model from one app to another

查看:182
本文介绍了将Django模型从一个应用程序移动到另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个Django应用程序中造成了太多的模型的愚蠢错误,现在我想把它分成3个不同的模型。问题是:在两个客户的网站上已经有数据生产了,所以我需要仔细地规划任何模式/数据迁移来完成(我使用的是django-south)。我不确定如何继续,任何建议将不胜感激。

I made the stupid mistake of creating too many models in the same Django app, now I want to split it into 3 distinct ones. Problem is: there's already data in production in two customers' sites, so I need to carefully plan any schema/data migration to be done (I'm using django-south). I'm unsure on how to proceed, any advice would be greatly appreciated.

(我在Ubuntu服务器上使用PostgreSQL 12.4 LTS,如果这有任何相关性)

(I'm using PostgreSQL on a Ubuntu server 12.4 LTS, if that's of any relevance)

我考虑过使用 db.rename_table ,但无法弄清楚如何正确地将外键更新到这些模型(旧到新) - 与数据库级无关(因为表重命名已经被覆盖了),但在ORM级别不是这样。

I thought about using db.rename_table, but can't figure out how to correctly update the foreign keys to those models (old to new) - irrelevant at the database level (since the table renaming already got that covered), but not so at the ORM level.

更新:在考虑之后,在询问这个问题在programmmers.SE,我决定保持简单,不用担心主要版本的产品之间的迁移。短期来说,我将使用 db.rename_table 匹配新的名称,同时使用 db_table 作为Daniel Roseman建议,所有的同时保持模型在旧的应用程序。当升级到主要版本时,我转向新的应用程序并完全移植所有迁移(因此,新版本的新安装将按原样创建数据库,而不是经历所有历史迁移)。

Update: after thinking about it, and after asking this question on programmmers.SE, I decided to keep things simple and don't worry about migrations between major versions of the product. Short term, I'll just use db.rename_table to match the new name, while also using db_table as Daniel Roseman suggested, all the while keeping the models in the old app. When upgrading to a major version, I swith to the new app and ditch all migrations altogether (so fresh installs of the new version will create the database "as-is" instead of going through all historical migrations).

推荐答案

我不明白为什么你需要任何数据迁移。

I don't see why you need any data migration at all.

新应用的模型,并添加 db_table 在内部Meta类中设置指向旧表名。

Just move the models to the new app, and add a db_table setting in the inner Meta classes to point to the old table names.

这篇关于将Django模型从一个应用程序移动到另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆