1

I try to build blog by using posts and MPTT comments, this will be in the home view www.mysite.com that's mean I cann't pass pk to the url so I tried to get the post objects using for loop

    comma = Post.objects.all()
    comm = []
    for post in comma:
        comment = PostCommentIDF.objects.filter(post=post)
        comm.append({"comme": comment}

   context = {'comment': comm,}
   return render(request, 'personal/home.html', context)

And my Mptt comment model

class PostCommentIDF(MPTTModel):
    post = models.ForeignKey(Post, on_delete=models.CASCADE,  related_name='pos_com')
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='post_children')
    author = models.ForeignKey(Account, on_delete=models.CASCADE)
    content = models.TextField()
    created_date = models.DateTimeField(auto_now_add=True)
    status = models.BooleanField(default=True)
    likes = models.ManyToManyField(Account, blank=True, related_name='pos_com')

My Post Model

class Post(models.Model):
    author = models.ForeignKey(Account, on_delete=models.CASCADE)
    article = models.TextField(null=True, blank=True)
    photo_article = models.ImageField(max_length=255, upload_to=get_poster_filepath)

My mptt coments in the template

   {% recursetree comment %}
      <div id="{{ node.id }}" class="my-2 p-2" style="border: 0px solid grey">
          <div class="d-flex justify-content-between">
              {{ node.publish|naturaltime }}
              <div class="node-content mt-3">{{ node.content  }}</div>
          </div>
      </div>
   {% endrecursetree %}
Ahmed
  • 280
  • 1
  • 12
  • 1
    Quick guess: `comment` is not a MPTTModel instance. But without the complete view it's difficult to go beyond that quick guess. – spectras May 12 '21 at 06:58
  • Hello thank you for your response, this is my view for get the post and pass it to `comment = PostCommentIDF.objects.filter(post=post)` – Ahmed May 12 '21 at 07:03
  • I added my context to the templated – Ahmed May 12 '21 at 07:14

1 Answers1

2

comment in your template context is a list of dictionaries. It should be a list of PostCommentIDF instances. And you are doing multiple SQL queries which is really inefficient. use __in operator instead and use the queryset directly, it's iterable too:

comma = Post.objects.all()
comment = PostCommentIDF.objects.filter(post__in=comma)

context = {'comment': comment,}
return render(request, 'personal/home.html', context)
yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
  • 1
    Hello It is work but ther is small problem this return all the comments in the db to any post, not ruturning the comments that related to specific post and thank you for your answer – Ahmed May 12 '21 at 07:52
  • If you want to retrieve comments only for a single post, you have to use the ID of the post in `filter()`, but I dont see you doing that anywhere in your original code neither. – yedpodtrzitko May 12 '21 at 09:00
  • I tried to get th pk from the `def comment(request, pk):` but it's didn't work because as I know that mean I have pass the pk to the url and I cann't because this is the home page wich dont have any extra url like this `www.mywebsite.com` – Ahmed May 12 '21 at 09:05
  • Seems like a completely separate problem out of scope for this question. Rather than trying to solve it by piggybacking here in comments I'd recommend to create a new question. – yedpodtrzitko May 12 '21 at 09:37
  • I made a new qustion https://stackoverflow.com/questions/67501293/how-i-can-get-the-pk-in-the-home-view-www-mywebsite-com – Ahmed May 12 '21 at 09:54