开发者

How to move data across tables with django?

开发者 https://www.devze.com 2023-03-25 05:37 出处:网络
Say I have three tables A, B, and C.A is the active table, B has a one2one relationship with A, and C is an archive table to store old A data.So in django I want to move a piece of data from A to C wh

Say I have three tables A, B, and C. A is the active table, B has a one2one relationship with A, and C is an archive table to store old A data. So in django I want to move a piece of data from A to C while being able to retain the one2one relationship with B.

So how can I do this?

Should I just copy the data to C开发者_如何学运维, delete the data in A? How can I maintain the relationship with the data with B? Is this done with a one2one with C and B? Does this mean I would have to key columns in B? And if I delete from A does django cascade delete in B?

Or is there a whole different way of moving around data from table to table?


Assuming A,B,C are 3 models declared in your app, you can simply make A and C have similar fields, and then copy the data from A to C, including the fkey field to B.

When you call delete in Django, it emulates the SQL constraint ON DELETE CASCADE, see https://docs.djangoproject.com/en/1.3/ref/models/querysets/#delete for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号