Hi Mike, You'll probably want to check-out select_related. [0]
With that feature, you could use: posts = Post.objects.select_related().filter(thread__slug=thread_slug) If the copy of Django you're using is recent enough (I don't know if it's in 0.96), you can use the 'depth' argument... posts = Post.objects.select_related(depth=2).filter(thread__slug=thread_slug) Also, if you want to reduce DB load you could have your ForeignKey to User directly, rather than Profile... but I don't know if you reference profile elsewhere. Good luck! -- Ben [0] http://www.djangoproject.com/documentation/db-api/#select-related On 4/6/07, Mike H <[EMAIL PROTECTED]> wrote: > > > Hi all, > > I'm implementing a simple forum which looks like this, but I'm > concerned about how many queries are going to get run, and if there is a > more efficient way o structuring my view : > > from django.contrib.auth.models import User > > class Profile(models.Model): > ... some attributes here, avatar, tagline etc. ... > user = models.ForeignKey(User) > > class Post(models.Model): > .. some forum info here, not important ... > poster = models.ForeignKey(Profile) > > So, say I am displaying a list of posts, and as part of the template I > want to output some information from their profile, and their username > which is stored in their user record. My view code looks something like > this : > > def thread(request, thread_slug): > posts = Post.objects.filter(thread__slug=thread_slug) > return render_to_response('posts.html',{'posts': posts,}) > > and my template looks like this: > > {% for post in posts %} > {{ post.poster.user.username }} said {{ post.content }} > {% endfor %} > > Is accessing the username from the user inside the poster inside the post > going to trigger a query for every post I display? Is there a better way > to access information that's a few levels deep like that? > > Thanks for any pointers you can give! > > Mike > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---