【问题标题】:import error when cross importing django/python [duplicate]交叉导入 django/python 时出现导入错误 [重复]
【发布时间】:2013-05-10 13:46:33
【问题描述】:

大家好,谁能帮我解决这个问题我在导入时遇到了导入错误,我认为这一切都发生了冲突。在我导入 B 之前一切都很好,因为我真的需要从 A 到 B 的 ForeignKey。

a/models.py

from b.models import B #there is the problem
Class A():
    ....
    b = models.ForeignKey(B) # I really have to do this

b/models.py

from c.models import C

Class B():
    ....
    c = models.ForeignKey(C)

c/models.py

from a.models import A
Class C():
    a = models.ForeignKey(A)

【问题讨论】:

    标签: python django


    【解决方案1】:

    你可以这样做(不导入模型B,只需键入格式为“app_name.model_name”的字符串)

    a/models.py

    Class A():
        ....
        b = models.ForeignKey("b.B")
    

    ForeignKey docs

    【讨论】:

    • 这很好用,谢谢一百万
    猜你喜欢
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多