-1

I'm building a flask application and need an dict from my database model. The query should filter by user_ids and only query a few rows. I guess the best way is to filter the rows and the use the internal __dict__ object to return my dict. Currently I know how to filter by ids, but I have no idea how to select specific rows. Users.query.filter(Users.id.in_(user_ids)).all()

the array of rows looks like rows = ["username", "role", "last_seen"]

any ideas how to do that?

1

1 Answer 1

3

load_only should do the job:

from sqlalchemy.orm import load_only
rows = ["username", "role", "last_seen"]
session.query(Users).filter(Users.id.in_(user_ids)).options(load_only(*rows)).all()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.