def who_went():
     return dict(records=SQLTABLE(db((db.trip_who.trip==db.trip.id)&
(db.who.id==db.trip_who.who)).select(orderby=db.trip.date|
db.who.display)))


On Dec 18, 12:55 pm, Chris Grindstaff <cgri...@gmail.com> wrote:
> Hi all,
> Just getting started here - I have a many to many relationship and I'm
> trying to do a join.
> I have 3 tables:
> db.trip contains: date, name, etc
> db.who contains: first, last, display
> db.trip_who combines trips and users with SQLField('trip', db.trip),
> SQLField('who', db.who))
>
> In my test controller I'm trying to list who went on a trip.
> The following works, but doesn't feel like idiomatic web2py. Any
> suggestion or improvements?
> I'm finding all trips. For each trip, consult the trip_who table to
> find the id of each person who went on the trip. Then consult the who
> table to find the display name of each person.
>
> def who_went():
>   all_trips = db().select(db.trip.ALL, orderby=db.trip.date)
>   for atrip in all_trips:
>     who = []
>     trip_q = db(db.trip_who.trip == atrip.id).select(db.trip_who.who)
>     for each in trip_q:
>       whos = db(db.who.id == each.who).select(db.who.display)
>       for each_who in whos:
>         who.append(each_who.display)
>     print 'trip: ', atrip.id, who
>
>   return dict(records=SQLTABLE(all_trips))

def who_went():
     return dict(records=SQLTABLE(db((db.trip_who.trip==db.trip.id)&
(db.who.id==db.trip_who.who)).select(orderby=db.trip.date|
db.who.display)))

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to