Try to reduce the queries on your view, select_related won't help a lot if using Thread model, try to collect Pun values on your view first and group them by thread, itertools.groupby could be handy in this case. Try something like:
threads = Thread.objects.select_related('user').filter(...) thread_ids = [th.id for th in threads] Pun.objects.select_related('thread', 'author').filter(thread__in=thread_ids) Then group Pun by thread and use that on your template. Makes sense? Regards, Matías Excerpts from Matt Henderson's message of Wed Jan 12 13:32:37 -0200 2011: > Hello, > I'm new to django, and set up a website recently, (puncut.com). I was hoping > someone might be able to explain how to optimise database calls because at > the moment some pages are taking 5 seconds before django responds. > I'll summarise the scenario below: > > # models > class Thread(models.Model) : > ... bunch of fields including title, description, author (linked to User) > > class Pun(models.Model) : > thread = models.ForeignKey(Thread) > author = models.ForeignKey(User) > ... other fields like content etc. > > class Like(models.Model) : > liker = models.ForeignKey(User) > pun = models.ForeignKey(Pun) > > So there are puns in threads, and users can like puns. > The view which lists latest threads is particularly slow. It sends threads > to the template, and the template loops through threads and inside it loops > through the puns which are in that thread. in pseudo code: > for thread in threads > for pun in thread.pun_set > output pun.content > But I reckon that makes way to many calls to the database. I think I ought > to use select_related , but I tried it and it didn't speed it up. > Thanks for any help, > Matt -- Matías Aguirre <matiasagui...@gmail.com> -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.