Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango template filters: counting objects
    text
    copied!<p>I have 2 classes in models.py Region and Survey. Survey have <code>region_select=model.ForeignKey(region)</code> and some other attributes and region just have <code>name=models.CharField</code>. 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:</p> <pre><code>&lt;..&gt; &lt;ul&gt; {% for Region in regions_list %} &lt;p&gt;&lt;b&gt;{{ Region }}&lt;/b&gt; &lt;i&gt;(count: {{ ??? }} )&lt;/i&gt;&lt;br&gt; {% for Survey in object_list %} {% if Survey.region_select = Region %} &lt;a href="{% url region-display Region.id %}"&gt;{{ Survey }}&lt;/a&gt; {% endif %} &lt;/p&gt; {% endfor %} {% endfor %} &lt;/ul&gt; &lt;..&gt; </code></pre> <p>region_list is queryset of <code>Region.objects.all()</code> same with object list (<code>Survey.objects.all()</code>) So far i get perfect list though i want to count those items separately for each region. I've tried <code>object_list.filter(region_select=Region).count</code> 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 <code>???</code> place? What about custom {{name|filter}} filters? again i just need that it would count objects... oh, i also tried adding method with same <code>Survey.objects.filter(region_select=Region).count</code> but it ended up writing a place and type of those objects(?!?).</p> <p>P.S. its not copy/paste so simple mistakes may have occurred while re-writing. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload