我如何只显示当前建议的评论?

时间:2013-09-29 23:27:43

标签: django django-views

我要做的是只在详细信息页面上显示当前建议的评论。但是我在VIEWS文件中编码的方式将显示所有注释。以下是我的观点:

def detail(request, suggestion_id):
    suggestion = get_object_or_404(Suggestion, pk=suggestion_id)
    comments = get_object_or_404(Comments.objects.all(), pk=suggestion_id) #This is my question
    if request.method == 'POST':
        commentform = CommentForm(request.POST)
        if commentform.is_valid():
            cd = commentform.cleaned_data
            comment = Comments.objects.get(sid=sid) 
            comment.comment = cd.get('comment')
            comment.save()
    commentform=CommentForm()
    context = {
        'suggestion' : suggestion,
        'commentform' : commentform,
        'comments' : comments,
    }

    return render(request, 'suggestionbox/detail.html', context)

模型:

class Suggestion(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    description = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
    status = models.ForeignKey('Status')
    category = models.ForeignKey('Category')

    def __unicode__(self):
        return self.title

class Comments(models.Model):
    comment = models.TextField()
    user = models.ForeignKey(User)
    pub_date = models.DateTimeField(auto_now_add=True)
    sid = models.ForeignKey(Suggestion) # Suggestion ID

    def __unicode__(self):
        return self.comment[:50]

我是Django的新手,我仍在努力弄清楚它是如何运作的。任何帮助将不胜感激。

0 个答案:

没有答案