0

I have a database containing a large amount of product-data. In order not to query for every product (here for the product images of the product with id 3), like this

images = session.query(Images.filename).filter(Images.product_id==3).all()

I want to query the entire table once, hold it in memory and filter the Query based on my needs, like this

images = session.query(Images)
productImage = [image.filename for image in list(images.filter(Images.product_id==3))]

This works, but there is probably a better way. Of course, the object images has to be filtered to contain the row(s) containing the relevant data for the product with id 3. How can I grab the columnvalues for the column filename from those rows more elegantly?

Mike
  • 1
  • Note that `images = session.query(Images)` does not issue a query to the DB. Only when you start iterating over a `Query` object in `list(images.filter(Images.product_id==3))` a query is emitted. – Ilja Everilä Dec 18 '17 at 13:44
  • Possible duplicate of [Flask SQLAlchemy query, specify column names](https://stackoverflow.com/questions/11530196/flask-sqlalchemy-query-specify-column-names) – Ilja Everilä Dec 18 '17 at 13:45

0 Answers0