Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用django标记获取每个对象内的对象总数_Python_Django - Fatal编程技术网

Python 使用django标记获取每个对象内的对象总数

Python 使用django标记获取每个对象内的对象总数,python,django,Python,Django,使用Django,我试图输出每个社区中的地块总数 一个社区有几个地段,有几个社区。下面是我得到的一个例子: Total lots: 99. 这个社区只有两块地。共有4个社区,共有9个地块 模型和视图似乎是正确的。我是否缺少一个过滤器,或者是否有一种不同的编写方法来获得正确的结果 {% if community.is_active %} <a class="panel-link" href="{% url 'community-detail' pk=community.id %}" %}

使用Django,我试图输出每个社区中的地块总数

一个社区有几个地段,有几个社区。下面是我得到的一个例子:

Total lots: 99. 
这个社区只有两块地。共有4个社区,共有9个地块

模型和视图似乎是正确的。我是否缺少一个过滤器,或者是否有一种不同的编写方法来获得正确的结果

{% if community.is_active %}
<a class="panel-link" href="{% url 'community-detail' pk=community.id %}" %}>
    <div class="col-md-6 community_col">
        <div class="community_box">
            <div class="row">
                <br>
                <div class="col-md-4 community_img">
                    <img src="/media/{{ community.logo }}" alt="">
                </div>
                <div class="col-md-7 col-md-offset-1">
                    <h1 style="margin-bottom: -10px; margin-top: -15px;"><small>{{ community.name }}</small></h1>
                    <h4>{{ community.city }}, {{ community.state }}</h4>
                    <h6>Total lots: {% for lot in community.lot_set.all %}{{ lots|length }}{% endfor %}</h6>
                    <h6>Total Active lots: </h6>
                    <h6>Total Sold lots: </h6>
                    <h6>Total Inactive lots: </h6>
                </div>
            </div>
        </div>
    </div>
</a>
{% endif %}
{% endfor %}
{%if community.is_active%}
{%endif%}
{%endfor%}
此逻辑:

{% for lot in community.lot_set.all %}{{ lots|length }}{% endfor %}
迭代每个批次对象。然后取
lots
的长度,据我所知,这在上下文中不是一个变量。如果只想计算对象的数量:

{{ community.lot_set.count }}
可以

但是,展望未来,您会希望获得活动、已售出、非活动等的计数,为了有效地进行此操作,您应该研究注释queryset并在数据库中进行此计数:

此逻辑:

{% for lot in community.lot_set.all %}{{ lots|length }}{% endfor %}
迭代每个批次对象。然后取
lots
的长度,据我所知,这在上下文中不是一个变量。如果只想计算对象的数量:

{{ community.lot_set.count }}
可以

但是,展望未来,您会希望获得活动、已售出、非活动等的计数,为了有效地进行此操作,您应该研究注释queryset并在数据库中进行此计数:


非常感谢您。这确实奏效了。现在,正如您所说的,在数据库中通过注释queryset来完成其余的计数似乎是合乎逻辑的。你能详细说明我将如何做到这一点吗?在模型或视图中?非常感谢。这确实奏效了。现在,正如您所说的,在数据库中通过注释queryset来完成其余的计数似乎是合乎逻辑的。你能详细说明我将如何做到这一点吗?在模型或视图中?