获取SQLalchemy instrumentedattribute的值

时间:2012-11-27 10:24:22

标签: python sqlalchemy

如何在SQLalchemy中获取InstrumentedAttribute对象的值:

(Pdb) ResultLine.item_reference_1
<sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x793dc90>

上面的语句打印出sqlalchemy对象。

我真正需要的是与之相关的价值。

2 个答案:

答案 0 :(得分:5)

没有与InstrumentedAttribute相关联的值;您正在查看表声明,而不是结果集。例如,InstrumentedAttribute用于定义与另一个表的关系。

在数据库中查询结果,生成的对象将为您填写引用:

 for res in session.query(ResultLine).filter(somefilter):
     print res.item_reference_1

答案 1 :(得分:0)

如果你有一个特定的项目,你可以得到这样的值:

line = session.query(ResultLine).first()

column = ResultLine.item_reference_1  # this is InstrumentedAttribute

key = column.key  # "item_reference_1"

value = getattr(line, key)