title: Need some advice for duplicated queries
body: <p>I have a lot of duplicated queries (in django debug toolbar) when i load my menu tabs, im sure i can optimize this but don't find the good way.  </p>
  
  <p>Models :</p>
  
  <pre><code>class Categorie(models.Model):
      name = models.CharField(max_length=30)
      visible = models.BooleanField(default = False)
  
      def __str__(self):
          return self.nom
  
      def getscateg(self):
           return self.souscategorie_set.all().filter(visible = True)
  
  
  class SousCategorie(models.Model):
      name = models.CharField(max_length=30)
      visible = models.BooleanField(default = False)
      categorie = models.ForeignKey('Categorie')
  
      def __str__(self):
          return self.name
  
      def gettheme(self): 
          return self.theme_set.all().filter(visible = True)
  
  
  class Theme(models.Model):
      name = models.CharField(max_length=100)
      visible = models.BooleanField(default = False)
      souscategorie = models.ForeignKey('SousCategorie')
  
      def __str__(self):
          return self.name
  </code></pre>
  
  <p>Views : </p>
  
  <pre><code>def page(request):
      categs = Categorie.objects.filter(visible = True)
  
      return render(request, 'page.html', locals())
  </code></pre>
  
  <p>Templates : </p>
  
  <pre><code>{% for categ in categs %}
  
      &lt;li&gt;
          {{categ.name}}
          &lt;ul&gt;
              {% for scateg in categ.getscateg %}
  
                  &lt;li&gt;
                      {{scateg.name}}
                      &lt;ul&gt;
  
                           {% for theme in scateg.gettheme %}
  
                               &lt;li&gt;{{ theme.name }}&lt;/li&gt;      
  
                           {% endfor %}                  
  
                      &lt;/ul&gt;             
                  &lt;/li&gt;
  
              {% endfor %}
          &lt;/ul&gt;
      &lt;/li&gt;
  
  {% endfor %}
  </code></pre>
  
  <p>I have look at prefetch_related but only work if i want load Categorie from SousCategorie and SousCategorie from Theme, so if i understand i need the contrary of this...</p>