getting error ..................The QuerySet value for an exact lookup must be limited to one result using slicing

Viewed 69

I'm building a social media website in Django. While I tried to list all the comments on the index page, I am getting this error, The QuerySet value for an exact lookup must be limited to one result using slicing and when I am using.... comments = PostComment.objects.filter(post__in=allPost)... instead of comments = PostComment.objects.filter(post=allPost) the QuerySet is not getting filtered and I am getting the same comments in all the post, basically, I want to show comments under all the post and that comment should be referred to as that post

things I have tried instead of comments = PostComment.objects.filter(post=allPost)

  1. comments = PostComment.objects.filter(post__in=allPost)

  2. comments = PostComment.objects.get(post__in=allPost)

  3. comments = PostComment.objects.get(post=allPost)

  4. comments = PostComment.objects.filter(post_id__in=allPost)

  5. comments = PostComment.objects.filter(post__id__in=allPost)

  6. comments = PostComment.objects.filter(post_id=allPost)

but none of them are working............................................please help me

what should I do in this case?

views.py...

def index(request):

    if request.user.is_authenticated:

        allPost = Post.objects.all().order_by('-created_on').filter(creater = request.user)

        allBlog = Blogpost.objects.all()

        comments = PostComment.objects.filter(post=allPost)

        context = {'allPost' : allPost, 'allBlog' : allBlog, 'comments' : comments}

        return render(request, 'index.html', context)
    
    else:
        return render(request, "signoption.html")

models.py....

 class Post(models.Model):

    sno = models.AutoField(primary_key=True)

    caption = models.CharField(max_length=500)

    hashtag = models.CharField(max_length=500)

    image = models.ImageField(upload_to='socialmedia/images', default="")

    created_on = models.DateTimeField(default=timezone.now)

    creater = models.ForeignKey(User, on_delete=models.CASCADE)


    def __str__(self):
        return str(self.sno) + '.....Post By.....' + str(self.creater)

class PostComment(models.Model):

    sno = models.AutoField(primary_key=True)

    comment = models.TextField()

    user = models.ForeignKey(User, on_delete=models.CASCADE)

    post = models.ForeignKey(Post, on_delete=models.CASCADE)

    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)

    created_on = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return str(self.sno) + '.....comment By.....' + str(self.user)

index.html....

{% for comment in comments %}

<div class="comment">

    <div class="comment-user">

        <div class="comment-usr-dp">

            <img src="{%static 'img/profile/profile.png'%}" alt="">

        </div>

    </div>

    <div class="comments-usr-usrname">

        <b><h1>{{comment.user.username}}</h1></b>

    </div>

    <div class="comment-text">

        <h1>{{comment.comment}}</h1>

    </div>

    <div class="comment-time">

        <h1>{{comment.created_on | naturaltime}}</h1>

    </div>
</div>
{%endfor%}

1 Answers
Related