I believe you'll want to use the options()
method on the Query, with eagerload()
or eagerload_all()
.
Here's an example of use from one of our apps, where the class Controlled
has a relation called changes
which brings in a bunch of DocumentChange
records, which themselves have a relation dco
that brings in one Dco
object per instance. This is a two-level eager-load, thus the use of the eagerload_all()
. We're using the declarative
extension (in case that matters) and m.Session is a "scoped" (thread-local) session.
from sqlalchemy.orm import eagerload, eagerload_all
...
controlled_docs = (m.Session.query(m.Controlled)
.options(eagerload_all('changes.dco'))
.order_by('number')
.all())
If that's not sufficient, perhaps include a snippet or text showing how the relevant ORM classes are related and I could update the answer to show how those options would be used in your case.