1

I am new in Django and I have been following tutorials online. I am having problem on how to display the comments. How do i query for comments in views, so i can display comments for a particular post.

Model:

class Post(models.Model):
    poster_profile = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, blank=True,null=True)
    image_caption = models.TextField(blank=True, null=True)

class Comments (models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, blank=True,null=True)
    commented_image = models.ForeignKey(Post, on_delete=models.CASCADE, null=True, blank=True)
    comment_post = models.TextField()

Views.py:

def home_view(request):
    comment = Comments.objects.all() #This is getting all comment in all post, how do i query for comment in a particular post.
    context{'comment':comment}
    return render(...)

Template:

{% for com in comment %}
<p>{{ com.comment_post }}</p>
{% endfor %}
ilhnctn
  • 2,210
  • 3
  • 23
  • 41
MrHize
  • 220
  • 5
  • 14

3 Answers3

4

You can do

post = Post.objects.get(id=1)
comment = post.comments_set.all()

Following Relationships "Backward"

If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the “Retrieving objects” section above.

Note this behaviour can be overridden.

You can override the FOO_set name by setting the related_name parameter in the ForeignKey definition. For example, if the Entry model was altered to blog = ForeignKey(Blog, on_delete=models.CASCADE, related_name='entries'), the above example code would look like this:

Edit #2:

views.py:

def home_view(request):
    posts = Post.objects.all().reverse()[5]

    context{ 'posts': posts, }
    return render(...)

Now in your templates you can do something like:

{% if posts %}
{% for post in posts %}
    {{ post.image_caption }}


    {% for comment in post.comments_set.all %}
        {{ comment.comment_post }}
    {% endfor %}

{% endfor %}
{% endif %}
Hisham___Pak
  • 1,472
  • 1
  • 11
  • 23
  • I got an error, Post matching query does not exist. – MrHize May 18 '20 at 15:30
  • I'm getting same error and it's pointing at, post = Post.objects.get(id=1) – MrHize May 18 '20 at 15:35
  • what about N+1 problem? – mdegis May 18 '20 at 15:35
  • Check the Post model, it does not have and id field. – mdegis May 18 '20 at 15:37
  • @Hisham.. Same error, Post matching query does not exist – MrHize May 18 '20 at 16:54
  • @Hisham.. It seems I'm getting the error because there is no post with id=1 – MrHize May 18 '20 at 18:48
  • @Hisham..I have watched lots of tutorials on YouTube, I'm now working on a website, just trying to create one for myself. I have list of posts by users, and each post have a comment form, just like Facebook new feeds. But comment is displaying in all post. – MrHize May 18 '20 at 20:04
  • @Hisham... Comment do not display. I know a related_name could do this, like: {% for comment in post.comments.all %} {{ comment.comment_post }} {% endfor %}. But is there a way I can query comment for each post without related_name? – MrHize May 18 '20 at 20:34
  • @Hisham...comment still do not display. Is there no way i can query for the id of the post? – MrHize May 18 '20 at 21:12
  • @Hisham___Pak..Thanks i will check it – MrHize May 18 '20 at 21:19
  • @Hisham... Not displaying comments – MrHize May 19 '20 at 15:52
  • @Hisham.. Still not displaying comments – MrHize May 19 '20 at 16:09
  • @Hisham.. Please help me out with this, when form is submitted using Ajax, it doesn't display comment, it just displays empty text, but in chrome console it displays the comment. I believe Ajax is working properly, but comment is not displaying. Please help me out. https://stackoverflow.com/questions/61768085/displaying-comments-using-ajax – MrHize May 20 '20 at 10:57
0

First of all, take a look at Django queryset documentation, especially select_related for this kind of issues (to reduce number of queries to database). I didn't try but following snippet must work.

class Post(models.Model):
    poster_profile = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    image_caption = models.TextField(blank=True, null=True)

class Comments (models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    commented_image = models.ForeignKey(Post, related_nam="comments", on_delete=models.CASCADE)
    comment_post = models.TextField()

...

def home_view(request):
    post = Post.objects.filter(id=request.data.get('post_id')).select_related("comments") # specify the post anyhow ..
    comments = post.comments
    context{'comment': comments}
    return render(...)
ilhnctn
  • 2,210
  • 3
  • 23
  • 41
-1
def home_view(request):
particular_Post= Post.objects.get(id=1)
    comment = Comments.objects.get(Post=particular_Post)
context{'comment':comment}
return render(...)

to understund Query in django i suggest U to start by

python manage.py shell

and Import you're Models