Also note, if your intentions were to grab all that information and
just send the artist to the template, I think you'd get better
performance if your queryset in the view looked like this:

queryset=Artist.objects.select_related().all()

Keith

On Aug 22, 11:17 am, lingrlongr <[EMAIL PROTECTED]> wrote:
> Super easy :)  Just to show you another way to implement generic
> views, I used custom view that returns a generic view.
>
> # urls.py
> from myapp.views import artist
> ...
> (r'^artist/(?P<slug>\w+)/$', artist),
> ...
>
> #views.py
> from django.views.generic.list_detail import object_detail
> def artist(request, slug):
>     return object_detail(
>         request,
>         queryset=Artist.objects.all(),
>         slug = slug,
>         template_object_name = 'artist'
>     )
>
> # <template_dir>/<app_name>/artist_list.html
> <h1>{{ artist }}</h1>
> <h2>Albums</h2>
> <ul>
> {% for album in artist.album_set.all %}
>    <li>{{ album.name }}</li>
>    <ul>
>    {% for track in album.track_set.all %}
>      <li>{{ track.name }}</li>
>    {% endfor %}
>    </ul>
> {% endfor %}
> </ul>
>
> Django makes traversing relationships easy...
>
> HTH
>
> Keith
>
> On Aug 22, 10:03 am, nek4life <[EMAIL PROTECTED]> wrote:
>
> > So if I sent the artist to the template and wanted to grab the list of
> > albums with all the album tracks how would I go about that.  Would I
> > have to pull in all the data with a custom view?  So far I've only
> > been using generic views.  It definitely makes sense pulling in the
> > information through the track back up through the album to the artist,
> > how could I reverse the process so I can get all the artist vars plus
> > the data I need from the track and album tables?  Thanks a bunch,
> > you've been very helpful already.
>
> > Charlie
>
> > On Aug 22, 12:26 am, lingrlongr <[EMAIL PROTECTED]> wrote:
>
> > > One more note.  You wouldn't NEED to explicitly grab all those vars,
> > > as you can get them in a template too.  I just wanted to show you the
> > > relation.
> > > If you sent the track to the template, you can get the artist by
> > > using:
>
> > > {{ track.album.artist }}
>
> > > Keith
>
> > > On Aug 22, 12:24 am, lingrlongr <[EMAIL PROTECTED]> wrote:
>
> > > > The only part you have that is redundant is the "artist" in your
> > > > "Track" class.  You can find out the artist because a track is related
> > > > to an album, which in turn, is related to an artist.
>
> > > > Some of the code you'd maybe see in a view would be:
>
> > > > # views.py
> > > > from django.shortcuts import get_object_or_404
> > > > from models import Album, Track
>
> > > > def album(request, slug):
> > > >   album = get_object_or_404(Album, slug=slug)
> > > >   artist = album.artist
> > > >   tracks = album.track_set.all()
> > > >   ...etc... return a response...
>
> > > > def track(request, slug):
> > > >   track = get_object_or_404(Track, slug=slug)
> > > >   album = track.album
> > > >   artist = album.artist
> > > >   ..etc..
>
> > > > HTH
>
> > > > Keith
>
> > > > On Aug 21, 11:44 pm, nek4life <[EMAIL PROTECTED]> wrote:
>
> > > > > I'm trying to set up my first Django application and I'm trying to
> > > > > figure out the database relationships.  I want to be able to list
> > > > > albums, with their corresponding tracks and album artwork.  Right now
> > > > > I only have foreign keys defined in the Track class and on the
> > > > > AlbumArt class pointing to the Album class.  I'm doing this so I can
> > > > > keep a record of which track or which album art goes to which album.
> > > > > However I also would like to add a ManyToManyField on my Album class
> > > > > so I can pull the album data in my view.  Defining this is both places
> > > > > seems redundant to me, but I'm not sure how else I can accomplish
> > > > > this.  What would be best practice in this situation and how should I
> > > > > proceed?
>
> > > > > class Album(models.Model):
> > > > >     title          = models.CharField(max_length=255)
> > > > >     prefix         = models.CharField(max_length=20, blank=True)
> > > > >     subtitle       = models.CharField(blank=True, max_length=255)
> > > > >     slug           = models.SlugField(unique=True)
> > > > >     artist         = models.ForeignKey('Artist')
>
> > > > > class AlbumArt(models.Model):
> > > > >     title          = models.CharField(max_length=200)
> > > > >     slug           = models.SlugField()
> > > > >     album          = models.ForeignKey('Album')
>
> > > > > class Track(models.Model):
> > > > >     title         = models.CharField(max_length=200)
> > > > >     slug          = models.SlugField(unique=True)
> > > > >     album         = models.ForeignKey('Album')
> > > > >     artist        = models.ForeignKey('Artist')
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to