On Thursday, August 15, 2013 6:57:23 AM UTC-4, Quint wrote:
> 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)?
>
Well, the most efficient way is probably to do a join, but that results in
a somewhat different type of object.
>
> 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.
>
I suppose you could also do something like that using Field.Virtual:
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
Or going in the other direction:
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
Note, don't define these virtual fields in both tables at the same time, as
that will lead to infinite recursion when attempting a select from either
one.
Anthony
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.