Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 炼金术关系内省_Python_Sqlalchemy_Introspection - Fatal编程技术网

Python 炼金术关系内省

Python 炼金术关系内省,python,sqlalchemy,introspection,Python,Sqlalchemy,Introspection,我有两个具有一对多关系的映射类: class Part(...): product = relationship('products', backref=backref('parts')) class Product(...): pass 给定Part.product,我可以反省这种关系,即获取属性名,也可以获取backref属性名: >>> rel = Part.product # image it's passed in as a function

我有两个具有一对多关系的映射类:

class Part(...):
    product = relationship('products', backref=backref('parts'))

class Product(...):
    pass
给定
Part.product
,我可以反省这种关系,即获取属性名,也可以获取backref属性名:

>>> rel = Part.product   # image it's passed in as a function parameter
>>> rel.property.key
'product'
>>> rel.property.backref[0]
'parts'
我也可以通过另一种方式访问关系:

>>> rel = Product.parts
>>> rel
<sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x3744fd0>
>>> rel.property.key
'parts'

我必须在哪里挠痒痒才能获得
“产品”

我试图重现您描述的情况,也得到了
Product.parts.property.backref=None
。 在pycharm中调试后,我发现其他属性在部分中包含属性名称:

print Product.parts.property.backref
>>>None
print Product.parts.property.back_populates
>>>product

我建议考虑使用<代码> Buffy填充< <代码>,作为黑客攻击。

中介绍了back_填充。根据文档,您需要这样定义您的模型:

class Part(...):
    product = relationship('products', back_populates='parts')

class Product(...):
    parts = relationship('products', back_populates='product')
    pass

我试图重现您描述的情况,也得到了
Product.parts.property.backref=None
。 在pycharm中调试后,我发现其他属性在部分中包含属性名称:

print Product.parts.property.backref
>>>None
print Product.parts.property.back_populates
>>>product

我建议考虑使用<代码> Buffy填充< <代码>,作为黑客攻击。

中介绍了back_填充。根据文档,您需要这样定义您的模型:

class Part(...):
    product = relationship('products', back_populates='parts')

class Product(...):
    parts = relationship('products', back_populates='product')
    pass
非常棒,正是我所需要的:-我甚至不认为这是一个黑客,它看起来像是查找属性名称的标准方法(BuffReF是一个设置后缀的快捷方式)。谢谢你弄明白了!非常棒,正是我所需要的:-我甚至不认为这是一个黑客,它看起来像是查找属性名称的标准方法(BuffReF是一个设置后缀的快捷方式)。谢谢你弄明白了!