Thanks!

Got it woking now but i got strange results:

("Not working" means that the same referenced record is being fetched every 
time and not read from the dict)

This is not working:

Field.Virtual('created_by_record',
                    lambda r: users.setdefault(r.comment.created_by.id, db.
auth_user(r.comment.created_by))),

This is also not working:

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),

But this is working:

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),






On Thursday, August 15, 2013 6:37:18 PM UTC+2, Anthony wrote:
>
> Good point. I suppose you can adapt your method to work with a virtual 
> field:
>
> owners = {}
> db.define_table('dog',
>     ...
>     Field.Virtual('owner_record',
>         lambda r: owners.setdefault(r.dog.owner, db.person(r.dog.owner)))
>
> That will store each fetched owner record in the owners dict, so the 
> record will be copied from the dict if it is already there rather than 
> pulled from the db.
>
> Anthony
>
> On Thursday, August 15, 2013 12:09:02 PM UTC-4, Quint wrote:
>>
>> 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.
>> On Aug 15, 2013 4:18 PM, "Anthony" <abas...@gmail.com> wrote:
>>
>>> On Thursday, August 15, 2013 10:04:45 AM UTC-4, Quint wrote:
>>>
>>>> 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?
>>>>
>>>
>>> Yes, that's exactly what the virtual field does -- when the the Rows 
>>> object is created from the initial select, the value of the virtual field 
>>> is calculated for each Row, and it remains in the Row (i.e., it is not 
>>> re-calculated on each access). There is also Field.Method(), which does 
>>> re-calculate on every access (the advantage is that it is lazy, so nothing 
>>> is calculated until accessed -- and because of that, it can take arguments 
>>> at the time it is called).
>>>
>>>  And about the join, wouldn't that mean it won't work on GAE?
>>>>
>>>
>>> Oh, yeah, forgot you said you are on GAE.
>>>
>>> Anthony
>>>
>>> -- 
>>>  
>>> --- 
>>> 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.
>>>
>>

-- 

--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to