title: Generate HTML Table that has Rows and Columns as Models in django with normal time complexity
body: <p>I would like to populate a table in a template, but I noticed that its extremely slow and i think that I am doing something terribly wrong (to be specific ... it take 3 seconds to handle one request from my localhost). I think that the whole reason of it being so slow is because of I am generating it over and over again ... which in my opinion is very bad thing to do ... sadly, I would like it to be pulled from DB in case of any problems (like server restart etc.) so thats why i do not want to initialise it as a global var and work with it that way.</p>
  <p><em><strong>models.py</strong></em></p>
  <pre><code>class Category(models.Model):
      name = models.CharField(max_length=120)
  
  
  class Note(models.Model):
      name = models.CharField(max_length=120)
  
  
  class Whistle(models.Model):
      name = models.CharField(max_length=120)
      category = models.ForeignKey(Category, on_delete=models.CASCADE)
      note = models.ForeignKey(Note, on_delete=models.CASCADE)
  </code></pre>
  <p><em><strong>views.py</strong></em></p>
  <pre><code>class ProductTableView(TemplateView):
      template_name = &quot;whistles_shop/table.html&quot;
  
      def get_context_data(self, *args, **kwargs):
          request = self.request
          context = super().get_context_data(**kwargs)
          context['whistle'] = Whistle.objects.all()
          context['note'] = Note.objects.all()
          context['category'] = Category.objects.all()
          all_rows = []
          for cat in Category.objects.all():
              one_row = [cat.name]
              for note in Note.objects.all():
                  whist = Whistle.objects.filter(category=cat, note=note)
                  if whist:
                      one_row.append(&quot;X&quot;)
                  else:
                      one_row.append(&quot;-&quot;)
              all_rows.append(one_row)
          context['tab'] = all_rows
          return context
  </code></pre>
  <p><em><strong>template.html</strong></em></p>
  <pre><code>     &lt;table class=&quot;table table-dark&quot;&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;#&lt;/th&gt;
                  {% for obj in note %}
                          &lt;th&gt;{{obj.name}}&lt;/th&gt;
                  {% endfor %}
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
          {% for row in tab %}
              &lt;tr&gt;
              {% for x in row %}
                  &lt;th&gt; {{x}} &lt;/th&gt;
              {% endfor %}
              &lt;/tr&gt;
          {% endfor %}
          &lt;/tbody&gt;
      &lt;/table&gt;
  </code></pre>
  <p>Any suggestions on how to fix the speed would be awesome &lt;3. Thanks in advance :)</p>