我有两个具有一对多关系的映射类:
class Part(...):
product = relationship('products', backref=backref('parts'))
class Product(...):
pass
给定Part.product
,我可以反省这种关系,即获得属性名,也可以得到backref属性名:
我也可以从另一个角度了解这种关系:
>>> rel = Product.parts
>>> rel
<sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x3744fd0>
>>> rel.property.key
'parts'
但是,我无法找到如何访问原始属性名(在示例中又称backref'backref属性,又称为“product”):
>>> rel.property.backref is None
True
我要在哪里挠Product.parts
才能得到{
我试着重现你描述的情况,也得到了
Product.parts.property.backref = None
。 在pycharm中调试后,我发现其他属性的部分名称为:我建议考虑在本例中使用
back_populates
作为黑客。在后台填充在documentation Linking Relationship Configuration:Relationships with Backref中描述。根据文档,您需要这样定义模型:
^{pr2}$相关问题 更多 >
编程相关推荐