Python Queryset按变量筛选,该变量为';她在另一个queryset

Python Queryset按变量筛选,该变量为';她在另一个queryset,python,django,django-views,Python,Django,Django Views,我试图通过另一个查询集中尚未设置的变量来过滤查询集。我知道这听起来很混乱,所以让我给你看看 Views.py def ViewThreadView(request, thread): posts = Post.objects.filter(post_thread=thread) thread = Thread.objects.get(pk=thread) form_class = QuickReplyForm thread_name = thread.name

我试图通过另一个查询集中尚未设置的变量来过滤查询集。我知道这听起来很混乱,所以让我给你看看

Views.py

def ViewThreadView(request, thread):
    posts = Post.objects.filter(post_thread=thread)
    thread = Thread.objects.get(pk=thread)
    form_class = QuickReplyForm
    thread_name = thread.name

  
    return render(request, 'thread/viewthread.html',
                  {'thread': thread, 'posts': posts, 'thread_name': thread_name})
后模型

class Post(models.Model):
    post_body = models.TextField(blank=True, null=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post_date = models.DateTimeField(auto_now_add=True)
    post_thread = models.ForeignKey(Thread, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.id) + ' | ' + str(self.author)

用户模型是标准的Django模型

现在,如果我想访问模板中的post-author,我会这样做

{% for post in posts %}
         post.author
{% endfor %}

我的问题是,如何访问post.author的表。因此,如果我想过滤作者有多少篇文章,我想做一些类似于
user\u posts=Post.objects.get(author=Post.author)
的事情。但这在视图中不起作用,因为“posts”是一个查询集,而不是一个值。如何执行此操作?

在模板中,您可以使用
post\u set
访问相关对象:

{% for post in posts %}
    {{ post.author.post_set.count }}
{% endfor %}
如果您需要的帖子数量超过了帖子总数,您是想筛选相关对象还是其他什么。始终可以为模型编写自定义方法。看

例如:

from django.utils.functional import cached_property


class Post(models.Model):
    post_body = models.TextField(blank=True, null=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post_date = models.DateTimeField(auto_now_add=True)
    post_thread = models.ForeignKey(Thread, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.id) + ' | ' + str(self.author)

    @cached_property
    def count_by_author_and_thread(self):
        return self.author.post_set.filter(post_thread=self.post_thread).count()
然后在模板中简单使用:

{% for post in posts %}
    {{ post.count_by_author_and_thread }}
{% endfor %}

在模板中,您可以使用
post\u set
访问相关对象:

{% for post in posts %}
    {{ post.author.post_set.count }}
{% endfor %}
如果您需要的帖子数量超过了帖子总数,您是想筛选相关对象还是其他什么。始终可以为模型编写自定义方法。看

例如:

from django.utils.functional import cached_property


class Post(models.Model):
    post_body = models.TextField(blank=True, null=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post_date = models.DateTimeField(auto_now_add=True)
    post_thread = models.ForeignKey(Thread, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.id) + ' | ' + str(self.author)

    @cached_property
    def count_by_author_and_thread(self):
        return self.author.post_set.filter(post_thread=self.post_thread).count()
然后在模板中简单使用:

{% for post in posts %}
    {{ post.count_by_author_and_thread }}
{% endfor %}

也分享你的
帖子
作者
模型。我继续添加了它,原来的帖子更新了我的答案。您还可以为特定查询编写自定义模型方法。还可以共享您的
帖子
作者
模型。我继续添加了它。原始帖子更新了我的答案。您也可以为特定查询编写自定义模型方法。我通过实现自己的模板过滤器以不同的方式解决了这个问题,但我认为这应该也能起到同样的作用。哪一个更有效?@Taziamomamabraham,正确有效的方法是使用模型方法。我通过实现自己的模板过滤器以不同的方式解决了它,但我认为这应该同样有效。哪个更有效?@Taziamomamabraham,正确有效的方法是使用模型方法。