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-args-kwargs [2] http://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model > > 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 -~----------~----~----~----~------~----~------~--~---