There may be a more elegant way to do this, but it does work.
Be sure to select the movie id in your query.
Then you can do something like this:
rows = db(query).select(.....) #whatever you're doing
trows = []
stars = []
for i, r in enumerate rows:
stars.extend([r.stars.name, BR()])
if r.movie.id != rows[i+1].movie.id or i+1==len(rows):
trows.append(TR(
TD(r.movie.name),
TD(stars), # Other stuff from the row follows
....
)
)
stars=[] # Reset the stars list
return(TABLE(*trows))
On Monday, August 13, 2012 6:04:44 PM UTC-4, Mike Girard wrote:
>
> I have a movie table that has a many-to-many relationship with a person
> table expressed through a star table.
>
> A simplified version of my model:
>
> db.define_table('movie',
>
> Field('title','string'),
>
> db.define_table('person',
>
> Field('name', 'string', unique=True),
>
> db.define_table('star',
>
> Field('movie_id', db.movie),
>
> Field('person_id', db.person),
>
> I am able to create a select that joins the three tables and produces a
> result with all the data I need.
>
> It's easy to iterate through the result and produce something akin to this:
>
> Movie Title Star 1
>
> Movie Title Star 2
>
> Movie Title Star 3
>
>
> What I want is:
>
> Movie Title 1
>
> Star 1, Star 2, Star 3
>
>
> Movie Title 2
>
> Star 1, Star 2, Star 3
>
>
> Programmatically, I'd like something like:
>
> for each movie in rows
>
> <h2>move.title</h2>
>
> <ul>
>
> for each star in movie.stars
>
> <li>star.name</li>
>
>
> Before I write a function to pre-process the result, can someone tell me
> if there is a helper for producing a result of this kind?
>
> This thread addresses the same issue -
>
> https://groups.google.com/forum/?fromgroups#!topic/web2py/GQsMt4qvqSs
>
> - but I was unable to discern the solution the question asker had produced
> for himself, the key to which was this:
>
> "web2py automatically add the many-to-many sets to an instance with the
>
> same name of the relation table"
>
> I do not know what that means.
>
--