为啥我的模型查询集不能正常工作?
Posted
技术标签:
【中文标题】为啥我的模型查询集不能正常工作?【英文标题】:Why isn't my Model Query set working properly?为什么我的模型查询集不能正常工作? 【发布时间】:2017-01-25 06:59:02 【问题描述】:我正在建立一个问答式社区,其中包含包含主题的论坛类别,每个主题都有帖子。在登录页面上,我想要所有类别的热门主题,当在一个类别中时,我想要该类别中的所有热门主题。我已经定义了模板过滤器来执行此操作,但它们没有在类别或主页级别显示任何内容。它们在论坛级别工作,即在特定论坛内,它能够获取该论坛中的所有主题。在一个类别中,它应该从该类别的每个论坛中获取所有主题,目前,它只返回该类别中的论坛列表,而不是所有主题的列表。在家庭级别,它应该返回所有类别和包含论坛的主题列表,但我什么也没得到。
谁能帮我弄清楚这里发生了什么:?谢谢
我的 TOPICSFORALL.html:
% block card_body %
% for topic in topic_list %
% include "subjectcard.html" with topic=topic %
% endfor %
% endblock %
我的 SUBJECTCARD.HTML:
% block card_body %
% for topic in topic_list %
% include "subjectcard.html" with topic=topic %
% endfor %
% endblock %
我的 SITE_BASE.HTML:
% extends "theme_bootstrap/base.html" %
% load i18n pybb_tags topicsbycat topicsbyforum %
% load static %
% catindexlist as catindexlisted %
% topicsbyall as topicsbyallcatnforum %
...
% block body %
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="col-xs-2 col-sm-3 col-md-2 col-lg-2" style="border-right:solid;text-align:right;height:99%;padding:0 0 0 0px;" id="sidebar">% include "categoryindex.html" %</div>
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" id="homebread">% include "breadcrumbbt.html" %</div>
% if category %
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">% include "topicstyleforumindex.html" %</div>
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">% include "topicsforall.html" with topic_list=category|topicsbycat %</div>
% elif forum %
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">% include "topicindex.html" %</div>
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">% include "topicsforall.html" with topic_list=forum|topicsbyforum %</div>
% elif topic %
<div class="main-block col-xs-6 col-xs-offset-1 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-2 col-lg-6 col-lg-offset-2" id="content" style="height:80%;">% include 'pybb/topic.html' %</div>
% else %
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">% include "topicstyleforumindex.html" %</div>
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">% include "topicsforall.html" with topic_list=topicsbyallcatnforum %</div>
% endif %
% endblock %
我的 PYBB_TAGS.PY:
@register.assignment_tag
def topicsbyall():
topic_list = Topic.objects.all().order_by('-updated', '-created')
return topic_list
我的 TOPICSBYFORUM.PY: 注册=模板.库()
@register.filter
def topicsbyforum(forum):
topic_list = Topic.objects.filter(forum=forum).order_by('post_count','views','-updated')
return topic_list
我的 TOPICSBYCAT.PY:
register = template.Library()
@register.filter
def topicsbycat(category):
topic_list = []
catforum = Forum.objects.filter(category=category).order_by('topic_count','post_count')
for forum in catforum:
forumtopics = Topic.objects.filter(forum=forum).order_by('post_count','views','-updated')
topic_list.append(forumtopics)
return topic_list
【问题讨论】:
【参考方案1】:我不确定到底是什么问题,但我通过将模板过滤器更改为仅在每个查询集上执行并在调用过滤器时在我的模板中使用更多 for 循环来让它工作。我猜过滤器在这方面有一些限制。我会尝试找到更具体的点来澄清这个问题并更新这篇文章,但在那之前,感谢尝试回答我问题的 6 个人!
【讨论】:
以上是关于为啥我的模型查询集不能正常工作?的主要内容,如果未能解决你的问题,请参考以下文章