title: Django debug toolbar says I have a query that is duplicate 1367 times
body: <p>I have a short view but with a complex query (the model that uses comes from a database MySQL view). The problem is that when this view executes, template loads too slow. The Django debug tool shows that a query is duplicated 1367 times, but the duplicated query has nothing to do with the complex database view query.  </p>
  
  <ol>
  <li>Is there any way to avoid duplicate query?  </li>
  <li>Is this delay because of the second query?  </li>
  </ol>
  
  <p>Below, is my template and view code. Sorry for not showing them too good, but it is my first post in StackOverflow.</p>
  
  <pre><code>def simbouloi_list(request, eklid):
      paramorder = request.GET.get('orderoption', '')
  
      try:
          paramorder = int(paramorder)
      except:
          paramorder = 6  # default ταξινόμηση
  
      selected_ekloges = Eklogestbl.objects.filter(eklid=eklid)
      # επιλογή όλων των εκλ. αναμετρήσεων με visible=1 και κάνω φθίνουσα ταξινόμηση  αν δεν δοθεί παράμετρος
      all_ekloges = Eklogestbl.objects.filter(visible=1).order_by('-eklid')
  
      #all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('surname', 'firstname', 'fathername')
  
      if paramorder==1 or paramorder==6:
          all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('surname', 'firstname','fathername')
      elif paramorder == 2:
          all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('sindiasmos', 'surname', 'firstname','fathername')
      elif paramorder == 3:
          all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('sindiasmos', 'toposeklogis', 'surname', 'firstname','fathername')
      elif paramorder == 4:
          all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by( 'toposeklogis','sindiasmos','surname', 'firstname','fathername')
      else:
          all_simbouloi = EklallsimbVw.objects.filter(eklid=eklid).order_by('toposeklogis', 'surname','firstname', 'fathername')
  
  
      context = {'all_ekloges': all_ekloges,
                 'selected_ekloges': selected_ekloges,
                 'all_simbouloi': all_simbouloi,
                 }
  
      return render(request, 'Elections/simbouloi_list.html' , context)
  </code></pre>
  
  <p>Template:</p>
  
  <pre><code>{% extends 'Elections/base.html' %}
  
  {% load staticfiles %}
  
  &lt;!-- καλούμε το base.html που προσθέτει το σταθερό Menu --&gt;
  
   {% block content %}
  
  
       {% include 'Elections/messages.html' %}
  
  
       &lt;div class="page-header"&gt;
  
              &lt;h3 align="center"&gt;Υποψήφιοι Σύμβουλοι (Δημοτικοί &amp; Τοπικοί)&lt;/h3&gt;
       &lt;/div&gt;
  
   &lt;div class="row"&gt;
  
         &lt;div class="col-md-10 col-md-offset-1"&gt;
  
                    &lt;form method="get" name="simbouloi"&gt;
  
                       &lt;select   name="orderoption"  onchange="simbouloi.submit()"&gt;
                       &lt;option  value="6" {% if selected_order ==  4  %}selected{%endif%}&gt;--------Επιλογή ταξινόμησης--------&lt;/option&gt;
                           &lt;option  value="1" {% if selected_order ==  1  %}selected{%endif%}&gt;Κατά Επώνυμο&lt;/option&gt;
                           &lt;option  value="2" {% if selected_order ==  2  %}selected{%endif%}&gt;Κατά Συνδυασμό/Επώνυμο&lt;/option&gt;
                           &lt;option  value="3" {% if selected_order ==  3  %}selected{%endif%}&gt;Κατά Συνδυασμό/Περ. Εκλογής/Επώνυμο&lt;/option&gt;
                           &lt;option  value="4" {% if selected_order ==  3  %}selected{%endif%}&gt;Κατά Περ. Εκλογής/Συνδυασμό/Επώνυμο&lt;/option&gt;
                           &lt;option  value="5" {% if selected_order ==  3  %}selected{%endif%}&gt;Κατά Περ. Εκλογής/Επώνυμο&lt;/option&gt;
  
                       &lt;/select&gt;
                       &lt;input type="text" class="heighttext" id="livesearch" placeholder="live search"&gt;&lt;/input&gt;
                    &lt;/form&gt;
  
          &lt;div class="divmargin"&gt; &lt;/div&gt;
  
                      &lt;table&gt;
  
                          &lt;tbody &gt;
  
                          &lt;tr class="noborder"&gt; &lt;td colspan="4"&gt;&lt;a href="{% url 'simbouloi_add' selected_ekloges.0.eklid%}" class="top-menu"&gt;&lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt; Νέα καταχώριση&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
  
                          &lt;/tbody&gt;
                     &lt;/table&gt;
  
                      &lt;div class="divmargin"&gt; &lt;/div&gt;
  
                     &lt;table class="table styletd1 table-condensed table-striped  table-hover"&gt;
  
                          &lt;tbody &gt;
                          &lt;tr&gt;
                                  &lt;td class="thick"&gt;Επώνυμο&lt;/td&gt;
                                  &lt;td class="thick"&gt;Όνομα&lt;/td&gt;
                                  &lt;td class="thick"&gt;Όν. Πατρός&lt;/td&gt;
                                  &lt;td class="thick"&gt;Περιοχή Εκλογής&lt;/td&gt;
                                  &lt;td class="thick"&gt;Συνδυασμός&lt;/td&gt;
                                  &lt;td class="thick"&gt;Ενέργειες&lt;/td&gt;
                          &lt;/tr&gt;
                          {% for p in all_simbouloi %}
                                &lt;tr&gt;
                                  &lt;td &gt; {{ p.surname }} &lt;/td&gt;
                                  &lt;td &gt; {{ p.firstname }}&lt;/td&gt;
                                  &lt;td &gt; {{ p.fathername }} &lt;/td&gt;
                                  &lt;td &gt; {{ p.toposeklogis }} &lt;/td&gt;
                                  &lt;td &gt; {% if  p.sindiasmos is None %} {% else %} {{ p.sindiasmos  }} {% endif %}&lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;a class="btn btn-default" href="{% url 'simbouloi_edit' selected_ekloges.0.eklid p.simbid%}"&gt;&lt;span class="glyphicon glyphicon-pencil"&gt;&lt;/span&gt;&lt;/a&gt;
                                      &lt;a class="btn btn-default" href="{% url 'simbouloi_delete' selected_ekloges.0.eklid p.simbid%}"&gt;&lt;span class="glyphicon glyphicon-minus"&gt;&lt;/span&gt;&lt;/a&gt;
                                  &lt;/td&gt;
  
                                &lt;/tr&gt;
                            {% endfor %}
  
                          &lt;/tbody&gt;
                     &lt;/table&gt;
  
                      &lt;br/&gt;
  
                      &lt;table&gt;
  
                          &lt;tbody &gt;
  
                          &lt;tr class="noborder"&gt; &lt;td colspan="4"&gt;&lt;a href="{% url 'simbouloi_add' selected_ekloges.0.eklid%}" class="top-menu"&gt;&lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt; Νέα καταχώριση&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
  
                          &lt;/tbody&gt;
                     &lt;/table&gt;
  
  
  
          &lt;/div&gt;
  &lt;/div&gt;
  
  
  
  
  {% endblock %}
  </code></pre>