I just added a prefetch function that should solve your problem. You can get the working code from the GitHub repository . This feature will be part of the upcoming Pony ORM 0.5.4 release.
Now you can write:
q = q.prefetch(Supplier)
or
q = q.prefetch(Order.supplier)
and Pony will automatically load the associated supplier objects.
Below I will show some prefetched queries using the standard Pony example with students, groups, and departments.
from pony.orm.examples.presentation import *
Loading only student objects without preliminary selection:
students = select(s for s in Student)[:]
Loading students with groups and departments:
students = select(s for s in Student).prefetch(Group, Department)[:] for s in students:
Same as above, but specifying attributes instead of objects:
students = select(s for s in Student).prefetch(Student.group, Group.dept)[:] for s in students: # no additional query to the DB is required print s.name, s.group.major, s.group.dept.name
Loading students and their courses (many-to-many relationships):
students = select(s for s in Student).prefetch(Student.courses) for s in students: print s.name for c in s.courses:
You can specify objects and / or attributes as parameters of the prefetch() method. If you specify an object, then all the one-to-one attributes with this type will be pre-programmed. If you specified an attribute, this special attribute will be preselected. The to-many attributes are preselected only when explicitly specified (as in Student.courses example). Prefetching is recursive, so you can load a long chain of attributes like student.group.dept .
When an object is preprogrammed, by default all its attributes are loaded, with the exception of lazy attributes and many attributes. You can pre-filter lazy and many attributes explicitly if necessary.
I hope this new method fully covers your use case. If something does not work properly, it will start a new problem on GitHub . You can also discuss features and perform feature requests on the Pony ORM mailing list .
PS I'm not sure if the repository template that you use gives you serious benefits. I think this actually increases the connection between rendering the template and the implementation of the repo, because you may need to change the implementation of the repo (i.e. add new entities to the prefetch list) when the template code starts using new attributes. With the top-level decorator @db_session you can simply send the result of the request to the template, and everything will happen automatically, without the need for explicit prefetching. But maybe I am missing something, so I will be interested to see additional comments about the benefits of using the repository template in your case.