On Tuesday, May 17, 2016 at 1:55:27 PM UTC-4, Alfonso Serra wrote: > > If i run: > subs = [s for s in g.subgroups.select() for g in db.company[1].groups. > select()] > i get an error 'g is not defined' >
It's not clear what final format you are looking for, but if you want a list of Row objects (each containing a subgroup), that is certainly doable. But if i run: > > >> [g for g in db.company[1].groups.select()] > > Then the former command does work: > > >> subs = [s for s in g.subgroups.select() for g in db.company[1].groups. > select()] > > Ok i guess this happens because lazy selects. > No, it's just incorrect Python code. You could nest the inner comprehension, but then there would be no point to the outer one. > In any case "i think" im performing 2 SQL instructions and 2 python loops. > just to get the subgroups > > Using joins, im performing 1 SQL instruction without loops. Thats why all > the hassle with the inheritance thing. > Correct, so rather than bother fixing the above comprehension code, which is inefficient, note that you *can *do a join in conjunction with a recursive select. Recall that db.company[1].groups is a DAL Set object, and you can add additional query conditions to a Set object by calling it (below assumes db.subgroups.group is the reference field to db.groups): subgroups = db.company[1].groups(db.subgroups.group == db.groups.id).select( db.subgroup.ALL) which is equivalent to: db((db.groups.company == 1) & (db.subgroups.group == db.groups.id)).select( db.subgroup.ALL) Anthony -- 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.

