在 South,我可以将旧列的值复制到新列吗?
我的 Django 模型之一是一个子类,我想将其超类更改为与原始模型非常相似的超类。特别是,新的超类描述相同的对象并具有相同的主键。如何让 South 创建新的 OneToOne 字段并将值从旧字段复制到新字段?
One of my Django models is a subclass and I want to change its superclass to one that is very similar to the original one. In particular, the new superclass describes the same object and has the same primary key. How can I make South create the new OneToOne field and copy the values from the old one to the new one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在南方,有两种迁移:模式迁移和数据迁移。
创建 schemamigration 后,创建相应的数据迁移:
./manage.py datamigration;
不要运行迁移(暂时)。相反,打开您刚刚创建的迁移文件。
您将找到名为
forwards()
的方法。您可以在其中定义将旧表中的值复制到新表的过程。如果要将给定表的结构更改为更复杂的布局,常见的方法是围绕数据迁移进行两次架构迁移:第一个架构迁移添加字段,数据迁移将旧字段转换为新字段,第二次架构迁移会删除旧字段。您可以使用
forwards()
方法对数据库执行任何操作,只要您跟踪正在访问的模式(以前的或当前的)即可。通常,您仅从orm.
相关的内容读取内容,并写入传统的 Django 访问器。South 数据迁移教程对此进行了一些详细介绍。它向您展示了如何在架构迁移之前使用 South 的
orm
引用来访问数据库,并提供对数据库的访问权限,而 Django 不会抱怨它不理解的字段。如果您要重命名一个类,这可能会很棘手——它涉及创建新表、从一个表迁移到另一个表以及删除旧表。 South 可以做到这一点,但可能需要多次转换模式和数据迁移。
South 还具有
backwards()
方法,该方法允许您将数据库表返回到上一步。在某些情况下,这可能是不可能的;新表可能会记录降级时会丢失的信息。如果您未处于调试模式,我建议您在backwards()
中使用抛出异常。In south, there are two kinds of migrations: schema migrations and data migrations.
After you've created the schemamigration, create a corresponding data migration:
./manage.py datamigration <app> <migration_name>
Do not run the migration (yet). Instead, open up the migration file you just created.
You'll find the method named
forwards()
. Into this you define the procedure by which values from old tables get copied to new tables.If you're changing the structure of a given table to a more complex layout, a common method is to have two schema migrations around a data migration: the first schema migration adds fields, the data migration translates the old fields to the new fields, and the second schema migration deletes the old fields. You can do just about anything with the database with the
forwards()
method, so long as you keep track of which schema (previous or current) you're accessing. Generally, you only read from theorm.
-related, and write to the traditional Django accessors.The South Data Migration Tutorial covers this in some detail. It shows you how to use South's
orm
reference to access the database using the schema prior to the schema migration and gives access to the database without Django complaining about fields it doesn't understand.If you're renaming a class, that can be tricky-- it involves creating the new table, migrating from one to the other, and deleting the old table. South can do it, but it might take more than one pass through shifting schemas and data migrations.
South also has the
backwards()
method, which allows you to return your database tables to a previous step. In some cases, this may be impossible; the new table may record information that will be lost in a downgrade. I recommend using throwing an exception inbackwards()
if you're not in DEBUG mode.