K, I think I have it.

Just having trouble getting the Author loop from within my Review
set.
Here's what I have below, I figured this was pretty close but I'm
wrong aren't I?

        {% for review in reviews_full %}
                <a href="">{{ review.item.title }}</a><br/>
                {{ review.item.publisher.name }}<br/>

                {% for author in review.item.author_set.all %}
                        {{ author.name }},
                {% endfor %}

                {{ review.pream }}
        {% endfor %}

Any ideas would be great.
Hope the above makes some sense ...

d



On Jul 2, 10:22 pm, Alex Robbins <alexander.j.robb...@gmail.com>
wrote:
> You should take a look at select_related[1]. It will take the lot of
> queries TiNo was talking about and flatten it down to one big one.
>
> [1]http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4
>
> Hope that helps,
> Alex
>
> On Jul 1, 1:34 pm, TiNo <tin...@gmail.com> wrote:
>
>
>
> > On Wed, Jul 1, 2009 at 16:59, The Danny Bos <danny...@gmail.com> wrote:
>
> > > Hi there,
>
> > > Seems easy, but I'm having an ass of a time. I think once I wrap my
> > > head around how to do this, I'll be rolling through Django like it's
> > > building sprites on a Commodore 64.
>
> > > So, I have four tables. Book, Publisher, Author and Review. A classic
> > > scenario.
> > > I want to display a loop on the home page showing all Reviews, display
> > > the Book.Title, Publisher.Name and Author.Name(s) among other things.
> > > One thing to note is, Publisher, Author, Book live in the 'books' APP
> > > and Review lives in the 'reviews' APP.
>
> > > Am keen to hear how to create a view and a template to display all of
> > > this linked data in one set.
>
> > A simple way would be to use:
>
> > #views.py:
> > reviews = Review.objects.all()
>
> > #template:
>
> > {% for r in reviews %}
> >     {{ r.item.title }}, {{ r.item.publisher.name }}
> >     {% for author in r.item.authors %}
> >         {{ author.name }}
> >     {% endfor %}
> > {% endfor %}
>
> > but that would create a lot of queries (1 for all Reviews + 3 per Review).
>
> > I don't know if it is possible, but maybe you could annotate [1] your
> > reviews with the associated values through the F() object [2] ? Just a wild
> > guess...
>
> > [1]http://docs.djangoproject.com/en/dev/ref/models/querysets/#annotate-a...
> > [2]http://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-r...
>
> > > Here are the models for each:
>
> > > class Publisher(models.Model):
> > >        name = models.CharField(max_length=120)
>
> > > class Author(models.Model):
> > >        name = models.CharField(max_length=120)
>
> > > class Book(models.Model):
> > >        title = models.CharField(max_length=120)
> > >        publication_date = models.DateField(blank=True, null=True)
> > >        authors = models.ManyToManyField(Author)
> > >        publisher = models.ForeignKey(Publisher)
>
> > > class Review(models.Model):
> > >        pream = models.TextField(blank=True)
> > >        body = models.TextField()
> > >        item = models.ForeignKey('books.Book')
--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to