Skip to content Skip to sidebar Skip to footer

Sqlalchemy Dynamic Query Using Object Attribute

I'm looking to query an object's attribute dynamically. I will not know which attribute, or column in this case, that I'll be using at the time of execution. class Product(Base):

Solution 1:

To dynamically access attributes, you should use the getattr builtin.

new_price = getattr(product, price_list.db_col_name)

If the instance is stale, you should use Session.expire, which means that the next time you access the attributes they will be retrieved from the database.

s.expire(product)

# or only expire price
s.expire(product, [price_list.db_col_name])

Post a Comment for "Sqlalchemy Dynamic Query Using Object Attribute"