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 <[email protected]> wrote: > On Wed, Jul 1, 2009 at 16:59, The Danny Bos <[email protected]> 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 [email protected] 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 -~----------~----~----~----~------~----~------~--~---

