【问题标题】:Fetch a value of SQLalchemy instrumentedattribute获取 SQLalchemy 检测属性的值
【发布时间】:2012-11-27 10:24:21
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python sqlalchemy


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      line = session.query(ResultLine).first()
      
      column = ResultLine.item_reference_1  # this is InstrumentedAttribute
      
      key = column.key  # "item_reference_1"
      
      value = getattr(line, key)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-30
        • 1970-01-01
        • 1970-01-01
        • 2013-05-22
        相关资源
        最近更新 更多