python - 传递 Django 语句或访问特定查询集索引
问题描述
我正在尝试返回属于该特定论坛的线程和帖子的数量。结构是这样的:类别 -> 论坛 -> 主题。
我想在数据库中查询属于该论坛的所有线程和帖子。
这是我当前的代码:
模型.py
class Forum(models.Model):
name = models.CharField(max_length=255)
description = models.CharField(max_length=255)
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='forums')
def __str__(self):
return self.name
class Thread(models.Model):
name = models.CharField(max_length=255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True)
thread_forum = models.ForeignKey(Forum, on_delete=models.CASCADE)
def __str__(self):
return self.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)
视图.py
class HomeView(ListView):
context_object_name = 'name'
template_name = 'index.html'
queryset = Category.objects.all()
def get_context_data(self, *args, **kwargs):
context = super(HomeView, self).get_context_data(*args, **kwargs)
context['thread_count'] = Thread.objects.all().count()
context['post_count'] = Post.objects.all().count()
context['user_count'] = User.objects.all().count()
context['categorys'] = Category.objects.all()
context['newest_user'] = User.objects.order_by('-id')[0]
return context
索引.html
{% for forum in category.forums.all %}
<div class="container-fluid category-forums clearfix"> <!-- 3 red -->
<div class="container-fluid category-forums-wrap d-flex align-items-center"> <!-- 4 green -->
<div class="container-fluid forum-details-wrapper clearfix">
<div class="container-fluid forum-details-container">
<p class="forum-name"><a href="{% url 'threadlist' forum.pk %}">{{ forum.name }}</a>
</p>
<p class="forum-desc">{{ forum.description }}</p>
<div class="sub-forum-container container clearfix">
<p class="sub-forums clearfix">{{ forum.category }}</p>
<p class="sub-forums clearfix">Sub-Category 2</p>
<p class="sub-forums clearfix">Sub-Category 3</p>
</div>
</div>
</div>
<div class="container-fluid forum-threads clearfix">
<div class="forum_threads_count">
100
</div>
<div class="forum_threads_threads">
<span>Threads</span>
</div>
</div>
<div class="container-fluid forum-posts clearfix">
<div class="forum_posts_count">
100
</div>
<div class="form_posts_text">
<span>Posts</span>
</div>
</div>
<div class="container-fluid forum-latest-posts clearfix">
<?php echo '<span class="last_thread"><a href=viewthread.php?id=''>Last Post</a></span>
<?php echo "<span class='last_post_by'>by Taz</span>
<span class="last_post">Last Post</span>
</div>
</div>
</div>
{% endfor %}
我希望我可以在 index.html 中执行此操作,但我不能:{{ Thread.filter(forum=forum).count()) }}
有什么想法可以简单地合并它吗?谢谢!
解决方案
<div class="forum_posts_count">
{{ forum.thread | length }}
</div>
尝试这个
推荐阅读
- javascript - 我在 django 中实现 css 和 js 动画文件时遇到问题
- python - 将频率“MS”设置为熊猫数据时间对象 - python
- c++ - -fshort-wchar 和 std::wstring - 分段错误
- c - 将多个动态流合并为一个
- python - RPG角色模拟器创建随机角色
- kotlin - 如何将字符串传递给 Kotlin 中的变量类型 URL?
- python - 在python中排序的“错误”字典的零索引位置插入列名作为(“错误”,“计数”)
- java - Exoplayer 线程错误 NoSuchElementExceptionat java.util.ArrayDeque.removeFirst
- amazon-web-services - 如何自动化 aws 托管 UI 的徽标
- vba - 在报表中使用 VBA 循环根据查询字段创建多个 PDF