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

查看:326
本文介绍了将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 ,但无法确定如何正确更新外键到这些模型(旧到新) - 在数据库级别无关(因为表已重命名

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.

更新:考虑后,在询问这个问题对程序员。我决定保持简单,不必担心主要版本之间的迁移。短期内,我将使用 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天全站免登陆