开发者

django template filters: counting objects

开发者 https://www.devze.com 2023-02-07 20:17 出处:网络
I have 2 classes in models.py Region a开发者_如何学Cnd Survey. Survey have region_select=model.ForeignKey(region) and some other attributes and region just have name=models.CharField. Using django gen

I have 2 classes in models.py Region a开发者_如何学Cnd Survey. Survey have region_select=model.ForeignKey(region) and some other attributes and region just have name=models.CharField. Using django generic views i make a list of items/region and i would like to have a filtered number of those items. My template looks like:

<..>
    <ul>
        {% for Region in regions_list  %}
            <p><b>{{ Region }}</b> <i>(count: {{ ??? }} )</i><br>
            {% for Survey in object_list  %}
                {% if Survey.region_select = Region %}
                    <a href="{% url region-display Region.id %}">{{ Survey }}</a>
                {% endif %}
            </p>
            {% endfor %}
        {% endfor %}
    </ul>
<..>

region_list is queryset of Region.objects.all() same with object list (Survey.objects.all()) So far i get perfect list though i want to count those items separately for each region. I've tried object_list.filter(region_select=Region).count but writing anything to filter ended up with template error saying that it could not parse what i give to filter. any suggestions what goes to ??? place? What about custom {{name|filter}} filters? again i just need that it would count objects... oh, i also tried adding method with same Survey.objects.filter(region_select=Region).count but it ended up writing a place and type of those objects(?!?).

P.S. its not copy/paste so simple mistakes may have occurred while re-writing.


Try:

Region.survey_set.all.count
0

精彩评论

暂无评论...
验证码 换一张
取 消