您好我想在模型中导入模型。但是,如果两个模型相互导入,我似乎无法做到这一点。下面要说明的是示例代码
# Files located in models with filename model_b.py
from repository.base_model import BaseModel
from models.model_a import ModelA
class ModelB(BaseModel):
# properties and classes here
# There's a join function here that make use of ModelA
现在我有另一个名为model_a的文件,它有一个ModelA类。我需要导入ModelB,因为我需要进行连接查询。
# Files located in models with filename model_a.py
from repository.base_model import BaseModel
from models.model_b import ModelB
class ModelA(BaseModel):
# properties and classes here
def join_function(code, session):
result = (session.query(ModelA).
select_from(ModelA).
join(ModelB, ModelB.id == ModelA.model_b_id).
filter(ModelB.code == code).
all())
如果我运行我的脚本,请说
ImportError:无法导入名称'ModelB'
现在,如果我删除了ModelB的导入,它就可以了。当两个模型相互导入时似乎存在问题。有解决方案吗?