<div dir="ltr"><div><div><div>Hi, Tomislav!<br><br>> Is it possible to make an entity always prefetch some related entities?<br><br></div><div>At this moment it is not possible. You can read fl.fullPath before exiting of db_session in order to force fetching of necessary related object.<br></div><div><br></div>When we using Pony in our current projects, we always construct JSON data (or some other kind of response) withing the db session, so it is not necessary to us to use the prefetch functionality at all. I think that the approach when db session wraps entire request processing is the most easy and efficient. But we can add the ability that you requested. It may be done in two different way.<br><br></div>1) We can add prefetch option to attribute:<br><br> directory = Required(lambda: Directory, prefetch=True)<div><br></div><div>2) We can add prefetch option to db_session:<br><br></div><div> with db_session(prefetch=[ File.directory ]):<br> fl = File[1]</div><div><br></div><div>Currently I prefer the second way more, because it allows to prefetch data only if it is explicitly requested, whereas the first variant requires to do prefetching in each db_session even if it is not necessary.<br></div><div><br><br>> 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()<br><br></div><div>We have a plan to add computed attributes, but our intention is different, we want to use such attributes inside queries, as a way to simplify complex expressions. That is, Pony should be able to translate such computed attributes to SQL and inline them into the query.<br></div><br></div>Until computed attributes are implemented, you can solve your current problem by overriding `to_dict` method and adding calculated values manually:<br><br> class File(database.Entity):<br>
filename = Required(unicode)<br> directory = Required(lambda: Directory)<br>
<br> @property<br>
def fullPath(self):<br>
return os.path.join(self.directory.path, self.filename)<br><div><div><br></div><div> def to_dict(self, *args, **kwargs):<br></div><div> result = super(File, self).to_dict(*args, **kwargs)<br></div><div> result['fullPath'] = self.fullPath<br></div><div> return result<br></div><div><br></div><div><br></div><div>Regards,<br></div><div>Alexander<br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 13, 2014 at 9:50 AM, Tomislav Tustonic <span dir="ltr"><<a href="mailto:ttustonic@outlook.com" target="_blank">ttustonic@outlook.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello<br>
<br>
Is it possible to make an entity always prefetch some related entities?<br>
For example, something like this:<br>
<br>
class Directory(database.Entity):<br>
path = Required(unicode, unique=True)<br>
files = Set(lambda: File)<br>
<br>
class File(database.Entity):<br>
filename = Required(unicode)<br>
directory = Required(lambda: Directory)<br>
<br>
@property<br>
def fullPath(self):<br>
return os.path.join(self.directory.<u></u>path, self.filename)<br>
<br>
# case 1<br>
with db_session:<br>
fl = File[1]<br>
print fl.fullPath<br>
<br>
# case 2<br>
with db_session:<br>
fl = File.select(lambda f: <a href="http://f.id" target="_blank">f.id</a>==1).prefetch(File.<u></u>directory).get()<br>
print fl.fullPath<br>
<br>
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?<br>
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.<br>
<br>
Thanks, Tom<br>
<br>
______________________________<u></u>_________________<br>
ponyorm-list mailing list<br>
<a href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a><br>
<a href="/ponyorm-list" target="_blank">https://mailman-mail5.<u></u>webfaction.com/listinfo/<u></u>ponyorm-list</a><br>
</blockquote></div></div>