like this?You run a query with an extra option specify the ref fields to pre-fetch. and then it would query all the referenced rows in a smart way (only fetch a record 1x).Is there a generally accepted way to handle this (in the absence of the mentioned feature)?
Right now I'm fetching my rows and loop over it and store the referenced rows in a dictionary and at the end use the dict to store the fetched rows on the parent rows.Then a pass the composite thing to my view.
db.define_table('person',
Field('name'),
Field.Virtual('dogs', lambda r: r.person.dog.select()))
db.define_table('dog',
Field('name'),
Field('owner', 'reference person'))
bob = db(db.person.name == 'Bob').select().first()
print bob.dogs
db.define_table('person',
Field('name'))
db.define_table('dog',
Field('name'),
Field('owner', 'reference person'),
Field.Virtual('owner_record', lambda r: db.person(r.dog.owner)))
spot = db(db.dog.name == 'spot').select().first()
print spot.owner_record.name
thnx,but my goal is to prevent repeated queries for the same record when accessing ref fields on rows from the same result set.I'm missing something, how do those Virtual Fields achieve that?
And about the join, wouldn't that mean it won't work on GAE?
But when when a row is calculating it's virtual field, it will fetch the referenced row even when some other row from the initial set has already fetched this same referenced row (when they are referencing the same thing). So they will both perform the same fetch.
?
I want to do this fetch 1x and use that record for all rows from the initial set that reference it.
--
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/IrAe-AGpiMU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
owners = {}
db.define_table('dog',
...
Field.Virtual('owner_record',
lambda r: owners.setdefault(r.dog.owner, db.person(r.dog.owner)))
Field.Virtual('created_by_record',
lambda r: users.setdefault(r.comment.created_by.id, db.auth_user(r.comment.created_by))),
users = {}
def created_by_record(r):
k = r.comment.created_by.id
return users.setdefault(k, db.auth_user(k))
Field.Virtual('created_by_record', created_by_record),
users = {}
def created_by_record(r):
k = r.comment.created_by.id
if k in users:
return users[k]
users[k] = db.auth_user(k)
return users[k]
Field.Virtual('created_by_record', created_by_record),
Appears that with set_default, the default param is always evaluated and this will perform the query every time guess..