Answer the question
In order to leave comments, you need to log in
How to display all "attached" comments in an article?
Greetings. Need help displaying all the comments on a page that are linked to a particular article.
Article Model:
class Article(models.Model):
title = models.CharField(max_length=255, verbose_name=u'Заголовок')
text = models.TextField(verbose_name=u'Текст')
author = models.ForeignKey(settings.AUTH_USER_MODEL)
rating = models.IntegerField(default=0, verbose_name=u'Рейтинг')
pub_date = models.DateField(auto_now_add=True)
class Comment(models.Model):
text = models.TextField(verbose_name='Текст')
pub_date = models.DateField(auto_now_add=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL)
article = models.ForeignKey(to=Article, on_delete=models.CASCADE)
def detail_art_view(request, a_id=1):
article = Article.objects.get(id=a_id)
comments = Comment.objects.all()
context = {'Article': article, 'com_list': comments}
return render(request, 'detail.html', context)
Answer the question
In order to leave comments, you need to log in
some_article.comment_set.all()
The name of the manager for feedback can be overridden by related_name:
And then:some_article.comments.all()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question