title: Looping through two models that contain lists and are connected Django
body: <p>I'm a starter with Python/Django and I'm currently trying to make a basic app for making a collection log. First the user chooses a category and then inserts objects under that category.</p>
  <p>I have two classes in models.py with the following code:</p>
  <pre><code>class object_category(models.Model):
  &quot;&quot;&quot;This class contains the users different categories&quot;&quot;&quot;
  CATEGORY_CHOICES = [
      ('category1', 'category1'),
      ('category2', 'category2'),
      ('category3', 'category3'),
  ]
  &quot;&quot;&quot;Plural for multiple categories&quot;&quot;&quot;
  class Meta:
      verbose_name_plural = 'Categories'
      
  &quot;&quot;&quot;Returns the above stated choices&quot;&quot;&quot;
  category = models.CharField(max_length=50, choices=CATEGORY_CHOICES)
  def __str__(self):
      return self.category
  
  class object_name(models.Model):
  &quot;&quot;&quot;This class contains the object name that is housed within a certain category&quot;&quot;&quot;
  &quot;&quot;&quot;Links the object to one of the chosen categories&quot;&quot;&quot;
  category = models.ForeignKey(object_category, on_delete=models.CASCADE)
  
  # Placeholder for connection with a plant database API
  object = models.CharField(max_length=50)
  
  &quot;&quot;&quot;Return the object input from the user&quot;&quot;&quot;
  def __str__(self):
      return self.object
  </code></pre>
  <p>and here is the views.py code:</p>
  <pre><code>def collection(request):
  &quot;&quot;&quot;The page that opens the collection of objects&quot;&quot;&quot;
  object_category = Object_category.objects.order_by('category')
  object_name = Object_name.objects.order_by('object')
  
  context = {
      'object_category': object_category,
      'object_name': object_name
  }
  
  return render(request, 'plntz_main/collection.html', context)
  </code></pre>
  <p>finnaly here is my html doc code:</p>
  <pre><code>{% for category in object_category %}
      {% for object in object_name %}
          &lt;h3&gt;{{ category }}&lt;/h3&gt;
              &lt;li&gt;{{ object }}&lt;/li&gt;
  {% empty %}
      &lt;li&gt;No category has been added yet.&lt;/li&gt;
      {% endfor %}
  {% endfor %}
  </code></pre>
  <p>This displays the category and the objects but they are not linked.</p>
  <p>What I'm trying to display is:</p>
  <ul>
  <li>The categories the user selected</li>
  <li>Each object in that specific category</li>
  </ul>
  <p>Can anyone explain how I alter the code do get this result ?</p>
  <p>Sorry is this is a mess. I'm new and this is my first try on a project and my first question on stackoverflow.</p>
  <p>Thanks!</p>