Yep, that seems to work. Thank you, Massimiliano!

On Sunday, May 10, 2015 at 5:15:53 AM UTC-5, Massimiliano wrote:
>
> Another way:
>
> tc = t.with_alias('chairs')
> q = (tc.name == 'chair') 
> q = q & (t.name == 'table') 
> q = q & (t.owner_id == tc.owner_id)
> q = q & (p.id == tc.owner_id)
>
> print db(q).select(*fields, distinct=True, orderby=p.first_name)
>
>
>
> On Sat, May 9, 2015 at 9:24 PM, Spokes <[email protected] <javascript:>> 
> wrote:
>
>> Thanks, Massimiliano. I was also considering an approach involving 
>> creating a list of IDs from the 'thing' table, but I'm not sure that this 
>> would be feasible in this case. They may be tens, possibly hundreds of IDs 
>> corresponding to the given criteria, and the grid based on these queries is 
>> part of the site's front page (i.e. it will be generated very often), so 
>> I'm not sure that this approach is necessarily efficient enough. I can't 
>> help thinking that there's a more straightforward and efficient way to 
>> accomplish this task.
>>
>>
>> On Saturday, May 9, 2015 at 8:14:51 AM UTC-5, Massimiliano wrote:
>>>
>>> Maybe something like that?
>>>
>>> db.define_table('person', Field('first_name'), Field('last_name'), 
>>> format='%(first_name)s')
>>> db.define_table('thing', Field('name'), 
>>>                          Field('owner_id', 'reference person'), 
>>>                          format='%(name)s')
>>>
>>> t = db.thing
>>> p = db.person
>>>
>>> fields = [p.first_name, p.last_name]
>>>
>>> q = ((t.owner_id == p.id) & (t.name == 'chair'))
>>> ids_people_with_chair = [a.id for a in db(q).select(p.id)]
>>>
>>> query = (t.owner_id == p.id)
>>> query = query & (t.owner_id.belongs(ids_people_with_chair))
>>> query = query & (t.name == 'table')
>>>
>>> print db(query).select(*fields, distinct=True, orderby=p.first_name)
>>>
>>>
>>>
>>> On Fri, May 8, 2015 at 10:49 PM, Spokes <[email protected]> wrote:
>>>
>>>> Hi, Dave. Yes, that seems to be an accurate summary of what I was 
>>>> attempting to do.
>>>>
>>>>
>>>>
>>>> On Friday, May 8, 2015 at 3:25:24 PM UTC-5, Dave S wrote:
>>>>>
>>>>>
>>>>>
>>>>> On Friday, May 8, 2015 at 11:55:13 AM UTC-7, Spokes wrote:
>>>>>>
>>>>>> I'd like to create a grid based on criteria relating to a primary 
>>>>>> table, and a table that references that primary table. Slightly 
>>>>>> modifying 
>>>>>> the example from the web2py docs, let's say the primary table is 
>>>>>> 't_person', and it's referenced by the table, 't_thing'. I'd like the 
>>>>>> grid 
>>>>>> to list 't_person' entries that are referenced by a 't_thing' entry with 
>>>>>> a 
>>>>>> value 'table' for the 'name' field, and by a 't_thing' entry with value 
>>>>>> 'chair' for the name field (that is, entries that meet both criteria, 
>>>>>> not 
>>>>>> one or the other). 
>>>>>>
>>>>>>
>>>>> Let's see if I understand this.  You want to use the set A of t_things 
>>>>> that have value 'table'  and the set B of t_things that have value 
>>>>> 'chair' 
>>>>> and find those members of t_person that are referenced by both sets?  
>>>>> Sort 
>>>>> of "for a in A where a.owner_id == b.owner_id for b in B, select p from 
>>>>> t_person where p.id == a.owner_id ?
>>>>>
>>>>> /dps
>>>>>
>>>>>  -- 
>>>> Resources:
>>>> - http://web2py.com
>>>> - http://web2py.com/book (Documentation)
>>>> - http://github.com/web2py/web2py (Source code)
>>>> - https://code.google.com/p/web2py/issues/list (Report Issues)
>>>> --- 
>>>> 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/d/optout.
>>>>
>>>
>>>
>>>
>>> -- 
>>> Massimiliano
>>>  
>>  -- 
>> Resources:
>> - http://web2py.com
>> - http://web2py.com/book (Documentation)
>> - http://github.com/web2py/web2py (Source code)
>> - https://code.google.com/p/web2py/issues/list (Report Issues)
>> --- 
>> 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] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Massimiliano
>  

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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/d/optout.

Reply via email to