【问题标题】:getting error ..................The QuerySet value for an exact lookup must be limited to one result using slicing出现错误............精确查找的 QuerySet 值必须限制为使用切片的一个结果
【发布时间】:2022-01-21 14:45:03
【问题描述】:

我正在用 Django 构建一个社交媒体网站。当我尝试在索引页面上列出所有 cmets 时,我收到此错误,精确查找的 QuerySet 值必须限制为使用切片的一个结果,并且当我使用时......comments = PostComment.objects.filter(post__in=allPost)... 而不是@987654322 @ QuerySet 没有被过滤,我在所有帖子中都得到了相同的 cmets,基本上,我想在所有帖子下显示 cmets,并且该评论应称为该帖子

我尝试过的东西,而不是 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)

但他们都没有工作...................................... ...请帮助我

在这种情况下我该怎么办?

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%}

【问题讨论】:

    标签: python html django django-models


    【解决方案1】:

    我如何看到这个问题The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error 有问题

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

    所以我认为你需要这样写

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

    【讨论】:

    • 我已经尝试过了,但它不再过滤,我仍然可以在所有帖子上看到相同的 cmets
    • 请帮帮我........
    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2020-11-13
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多