2024-10-04 09:21:20 发布
网友
例如,我有两个应用程序:alpha和beta 在阿尔法/模型.py从模型导入beta.型号 在测试版/模型.py从导入模型阿尔法模型在
在管理.pyvalidate说ImportError:无法导入名称ModelName
如何解决这个问题?在
我以前遇到过这个问题有两个模型互相引用,即使用ForeignKey字段。根据Django documentation,有一种简单的方法来处理它:
ForeignKey
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') ...
此时,从阿尔法模型没有必要。在
我以前遇到过这个问题有两个模型互相引用,即使用
ForeignKey
字段。根据Django documentation,有一种简单的方法来处理它:所以在你的测试版中/模型.py模特,你会有这个:
此时,从阿尔法模型没有必要。在
相关问题 更多 >
编程相关推荐