On Dec 26, 11:46 pm, "Matthias Merk" <[EMAIL PROTECTED]> wrote:
Hi

I need some tips on this please :)
I have a simple artist/release model with a ManyToOne relationship
(every artist publishing different pieces)

whats the best way to display this with datagrid or any other way?
The artist_fields datagrid returns a tuple or list and i couldn't find
a way to display it nicely in the template.
I'd like to have it all in one table, each artist and the matching
releases.
[snip]
some guidance or reading tips would be much appreciated
def index(self):
        artist_fields = [('Name','name'),
                         ('Releases','releases')]
        release_fields = [('Name', 'name'),
                         ('Date', 'date'),
                         ('Size', 'size'),
                         ('Description', 'description')]

        return dict(artists=Artist.select(),

artist_widget=widgets.DataGrid(fields=artist_fields),
                    releases=Release.select(),

release_widget=widgets.DataGrid(fields=release_fields))

How are you rendering the widgets in yout template? Should be something
like

${artist_widget.display(artists)}
${release_widget.display(releases)}

That should give you two seperate tables, one of artists and one of
releases.

But you are after both bits information in a single table, which is a
little bit more work. Datagrid fields can be callable functions (which
are passed each result in turn from the select() list).  So you can
write a function that would grab an artist's releases and put their
names in a comma seperated list, for example.

def get_releases(artist):
   return ', '.join(r.name for r in artist.releases)

def index(self):
   artist_fields = [('Name','name'),
                         ('Releases', get_releases)]
   return dict(artist_widget=widgets.DataGrid(fields=artist_fields),
                  artists=Artist.select())

and

${artist_widget.display(artists)}

in the kid template.

You can put whatever you wanted as the return value of that function.
If you wanted you could even display another nested datagrid with the
releases for each artist within the main datagrid.

Eg.

def get_releases(artist):
   release_fields = [('Name', 'name'),
                           ('Date', 'date'),
                           ('Size', 'size'),
                           ('Description', 'description')]

   return DataGrid(fields=release_fields).display(artist.releases)

Note: this doesn't return a string, it returns an ElementTree generator
(which is what  the display method returns), which is another input
type that kid understands and can render to HTML. If you return raw
HTML as a string, kid will escape it, which you can avoid by wrapping
your HTML string in a call to kid.XML() if thats what you need to do.
Personally, I just use widgets, that's their purpose :)

HTH

--
wavydavy


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to