title: Django: How to iterate over two one-two-many table relationships?
body: <p>I am trying to build a simple web page that queries three tables. There is a <code>Company</code> table that has a one-to-many relationship with a <code>Position</code> table, as well as a one-to-many relationship with a <code>Project</code> table. </p>
  
  <p>The goal is to have the page display a given company once, along with all positions and and projects associated with said company. Then, move on to display the next company, any positions held there and projects completed. </p>
  
  <p>Below is the closest I've come to getting this right. But, the obvious problem is that if there is more than one project associated with a given company, you'll see that company listed more than once.</p>
  
  <p>I'm new to Django, so in the interest of learning, I wanted to beat my own head sufficiently hard before asking for help; but I could really use some fresh ideas at this point. </p>
  
  <p>Also: I can see how a nested for loop might work here, but I'm just not clear on how the mechanics of that would work with the query, and then within the template. </p>
  
  <p>Models:</p>
  
  <pre><code>from django.db import models
  
  
  class Company(models.Model):
      company_id = models.AutoField(primary_key=True)
      company_name = models.CharField(max_length=20)
      company_logo = models.ImageField(upload_to='images/')    
  
      def __str__(self):
          return self.company_name   
  
  class Position(models.Model):
      position_id = models.AutoField(primary_key=True)
      position_title = models.CharField(max_length=55)
      company_id = models.ForeignKey('professional.Company',
                                 on_delete=models.CASCADE,
                                 blank=True,
                                 null=True)
      begin_date = models.DateField()
      end_date = models.DateField()   
  
      def __str__(self):
          return self.position_title   
  
  class Project(models.Model):
      project_id = models.AutoField(primary_key=True)
      project_name = models.CharField(max_length=55)
      company_id = models.ForeignKey('professional.Company',
                                 on_delete=models.CASCADE,
                                 blank=True,
                                 null=True)
      project_description = models.CharField(max_length=500)
      project_image = models.ImageField(upload_to='images/')
  
      def __str__(self):
          return self.project_name
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>from django.views.generic import TemplateView, ListView
  
  from professional.models import Company
  
  
  
  class ProfessionalHome(TemplateView):
      template_name = 'professional/professional_home.html'
  
  
  class TechnologyListView(ListView):
      template_name = 'professional/__technology.html'
      context_object_name = 'technology_list'
  
      def get_queryset(self):        
          return Company.objects.values('company_name','position__position_title', 'project__project_name')
  </code></pre>
  
  <p>HTML and template:</p>
  
  <pre><code>{% for job in technology_list %}
  
      &lt;h1&gt;{{job.company_name}}&lt;/h1&gt;
      &lt;h1&gt;Position: {{job.position__position_title}}&lt;/h1&gt;
      &lt;h1&gt;project: {{job.project__project_name}}&lt;/h1&gt;
  
  {% endfor %}
  </code></pre>