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!