Taking this exact example

Foreach movie in movies
    print movie.title
    foreach stars in movie.stars
       print star.name


I guess `movie.stars` is a list:string field?

if so.. there's one-liner solution

table = TABLE(*[TR(TD(movie.title), TD(UL(*[LI(star.name) for star in
movie.stars]))) for movie in movies])

if db.stars has a list:reference to movies

table = TABLE(*[TR(TD(movie.title),
                            TD(UL(*[LI(star.name) for star in
db(movie.id.belongs(db.stars.movies)).select(db.stars.name)]))) for movie
in movies])

or with a subquery (for each loop iteration)

table = TABLE(*[TR(TD(movie.title),
                            TD(UL(*[LI(item.stars.name) \
                                           for item in *db((db.movies.id ==
db.stars.id) & \*
*                                                               (
db.movies.id == movie.id)).select(db.stars.name)*]))) \

                                                      for movie in movies])


On Mon, Aug 13, 2012 at 9:51 PM, Cliff Kachinske <cjk...@gmail.com> wrote:

> 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<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.
>>
>  --
>
>
>
>

-- 



Reply via email to