tags:

views:

49

answers:

3

Hello,

I am trying to learn Django (and Python) and have created my own app based on the Tutorial found in Djangoproject.com.

My model is like this:

from django.db import models

class Blog(models.Model):
    author = models.CharField(max_length=200)
    title = models.CharField(max_length=200)
    content = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
            return self.content

    def was_published_today(self):
            return self.pub_date.date() == datetime.date.today()

class Comment(models.Model):
    blog = models.ForeignKey(Blog)
    author = models.CharField(max_length=200)
    comment = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
            return self.comment

Then in my views.py, I have this:

from django.shortcuts import render_to_response
from django.http import Http404
from mysite.blog.models import Blog
from mysite.blog.models import Comment

def index(request):
    blog_posts = Blog.objects.all().order_by('-pub_date')[:5]
    return render_to_response('blog/index.html', {'blog_posts': blog_posts})

def post(request,blog_id):
    try:
        post = Blog.objects.get(pk=blog_id)
        #Below, I want to get the comments
        #----------------------------------------------------------
        comments = Comment.objects.select_related().get(id=blog_id)
        #----------------------------------------------------------

    except Blog.DoesNotExist:
        raise Http404
    return render_to_response('blog/post.html', {'post': post, 'comments': comment})

My question is, how do I get the comments related to the blog post?

comments = Comment.objects.select_related().get(id=blog_id)

In the most basic terms of PHP, I would have simply executed a query that looks something like this:

$sql = "SELECT c.* FROM comments c WHERE c.blog_id=6"

Can anyone help me or at least guide me to the correct documentation for this?

Thanks so much!

+1  A: 

http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

reading the documentation will help you immensely and save you tons of time

edit: actually i am not sure thats the right area but what you want to do is get the Blog object

b = Blog.objects.get(id=1) then

comments = Comments.objects.filter(blog=b)

jacob
Thanks so much! Exactly what I am looking for.
wenbert
@Daniel Roseman shows the even easier way to do this.
Carl Meyer
+1  A: 

You could also query by id alone, using the id of the entry. But since you need post in your case you'll probably want to use jacob's suggestion. :

def post(request, blog_id):
    try:    
        comments = Comments.objects.filter(blog__pk=blog_id)
    .
    .
    .

Refer to Lookups that span relationships for details.

monkut
+2  A: 

If you've already got the blog object, you can also get the related comments like this:

comments = blog.comment_set.all()
Daniel Roseman