Skip to content Skip to sidebar Skip to footer

Fetch A Value Of Sqlalchemy Instrumentedattribute

How can I fetch the value of a InstrumentedAttribute object in SQLalchemy: (Pdb) ResultLine.item_reference_1

Solution 1:

There is no value associated with an InstrumentedAttribute; you are looking at a table declaration, not a result set. InstrumentedAttribute is used to define a relationship to another table, for example.

Query the database for results, and the resulting objects will have the reference filled in for you:

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

Solution 2:

if you have a specific item, you can get the value like this:

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

column = ResultLine.item_reference_1  # this is InstrumentedAttributekey = column.key  # "item_reference_1"value = getattr(line, key)

Post a Comment for "Fetch A Value Of Sqlalchemy Instrumentedattribute"