[PonyORM-list] Always prefetch some related entities ?

Tomislav Tustonic ttustonic at outlook.com
Sat Dec 13 06:50:05 UTC 2014


Hello

Is it possible to make an entity always prefetch some related entities?
For example, something like this:

class Directory(database.Entity):
     path = Required(unicode, unique=True)
     files = Set(lambda: File)

class File(database.Entity):
     filename = Required(unicode)
     directory = Required(lambda: Directory)

     @property
     def fullPath(self):
         return os.path.join(self.directory.path, self.filename)

# case 1
with db_session:
     fl = File[1]
print fl.fullPath

# case 2
with db_session:
     fl = File.select(lambda f: f.id==1).prefetch(File.directory).get()
print fl.fullPath

Case 1 will not work, case 2 will. So, I'd like to be able to specify that 
getting File always loads Directory, so that fullPath is available outside 
db_session. Is it (or will it be) possible?
Somewhat related to this, it would be nice to be able to have 'calculated' 
attributes, which wouldn't be saved and loaded from the database, but would be 
serialized using to_dict(). I don't think it's possible now.

Thanks, Tom



More information about the ponyorm-list mailing list