0

Looking to conveniently select some data into a dictionary keyed by specified columns, like so:

# pseudocode
results = session.query(MyModel).all(index_result_by=MyModel.id)
print(results.__class__)
<class 'dict'>
print(results[5])
<MyModel 5>

I could use a loop or dict comprehension, but is there anything built in?

1 Answer 1

1

Just select tuples of id, instance and pass to dict():

results = dict(session.query(MyModel.id, MyModel).all())
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!!! Way cool, I didn't know you could combine columns and full objects like that.

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.