djang交叉进口

2024-10-04 09:21:20 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,我有两个应用程序:alpha和beta 在阿尔法/模型.py从模型导入beta.型号 在测试版/模型.py从导入模型阿尔法模型在

在管理.pyvalidate说ImportError:无法导入名称ModelName

如何解决这个问题?在


Tags: py模型alpha名称应用程序beta型号importerror
1条回答
网友
1楼 · 发布于 2024-10-04 09:21:20

我以前遇到过这个问题有两个模型互相引用,即使用ForeignKey字段。根据Django documentation,有一种简单的方法来处理它:

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:

所以在你的测试版中/模型.py模特,你会有这个:

class BetaModel(models.Model):
    alpha = models.ForeignKey('alpha.AlphaModel')
    ...

此时,从阿尔法模型没有必要。在

相关问题 更多 >