在Django(1.8)应用程序之间移动模型与ForeignKey引用 [英] Move models between Django (1.8) apps with ForeignKey references

查看:383
本文介绍了在Django(1.8)应用程序之间移动模型与ForeignKey引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有同样的问题移动Django(1.8)应用程序与所需的ForeignKey引用之间的模型。 @halfnibble
的响应似乎是有效的,但在我的情况下,有一个ForeignKey从new_app指向old_app。
为了简单起见,我将把我的名字翻译成@halfnibble的名字,轮胎将有一个ForeignKey to Car。



迁移运行正常。
稍后迁移我将old_app.Car重命名为old_app.NewCar(我不知道这是否影响)。
但有些迁移后,当我尝试删除new_app.Tires,并在下一次迁移old_app.NewCar我得到错误:

 > python manage.py migrate 
....
ValueError:模型的未处理的挂起操作:
old_app.newcar(由字段:new_app.Tires.car引用)

如果我运行我的应用程序测试,它们正常运行,我也检查了生成的MySQL查询,似乎是正确的。 p>

解决方案

我发现错误,我留下这些信息,以防任何人都有同样的问题。



这个问题真的是由old_app.Car重命名为old_app.NewCar引起的。



makemigrations检测到更改,并在old_app中创建了两个迁移重命名等new_app更新ForeingKey。问题是您必须手动添加new_app依赖关系old_app迁移模型重命名。


I basically have the same question Move models between Django (1.8) apps with required ForeignKey references. The response from @halfnibble seems valid but in my case there is a ForeignKey pointing from new_app to old_app. I wil translate my names to names on @halfnibble example for simplicity, Tires will have a ForeignKey to Car.

The migrations runs fine. Few migrations later I rename old_app.Car to old_app.NewCar (I don't know if this affects). But some migrations later when I try to delete new_app.Tires and in next migration old_app.NewCar I get the error:

>python manage.py migrate
  ....
ValueError: Unhandled pending operations for models:
  old_app.newcar (referred to by fields: new_app.Tires.car)

If I run my apps tests they run correctly, I have also inspected generated MySQL queries and seems correct.

解决方案

I found the error, I left this information in case anybody has the same problem.

The problem was really induced by the rename of old_app.Car to old_app.NewCar.

makemigrations detected the change and created two migrations one in old_app with the rename and other on new_app updating the ForeingKey. The problem was that you have to manually add in new_app dependencies the old_app migration where the model was renamed.

这篇关于在Django(1.8)应用程序之间移动模型与ForeignKey引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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