title: Django template filters 2 ManytoMany fields
body: <p>I am writing an application in Django that takes all of the athletes in an event, sorts them into categories (lightweight, heavyweight, etc), and then displays the athletes in each category sorted from the top scoring athlete to the bottom scoring athlete. I can't get my template to sort the athletes into classes; either it displays all of the athletes or none.</p>
  
  <p>Here are the relevant sections of my models and template:</p>
  
  <p>An athlete may be in multiple categories and each category has multiple athletes. Also the categories are ordered by date.</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class Entry(models.Model):
      athlete = models.ForeignKey(Athlete, related_name='entries')
      event = models.ForeignKey(Event, related_name='entries')
      athlete_category = models.ManyToManyField(Category, related_name='entries')
      athlete_score = models.CharField(max_length=20, blank=True)
      placing = models.SmallIntegerField(blank=True, null=True)
  
  class Category(models.Model):
      category_name = models.CharField(max_length=100)
  
  class CategoryOrder(models.Model):
      event = models.ForeignKey(Event)
      category = models.ForeignKey(Classes)
      category_order = models.SmallIntegerField()
  </code></pre>
  
  <p><strong>event_placings.html</strong></p>
  
  <pre><code>{% for category in categories %}
      &lt;p&gt;{{ category.category_name }}&lt;/p&gt;
          {% for entry in entries %}
              {% for athlete_category in entry.athlete_category %}
                  {% if athlete_category == category %}
                      &lt;p&gt;{{ entry.athlete.first_name }} {{ entry.athlete.last_name }} - {{ entry.placing }}&lt;/p&gt;
                  {% endif %}
              {% endfor %}
          {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>The template is supposed to list each of the categories and then all of the athletes in that category based on his placing. The output should be:</p>
  
  <p>Men's Lightweight<br />
  John Doe - 1<br />
  Joe Public - 2</p>
  
  <p>Women's Lightweight<br />
  Jane Doe - 1<br />
  Eva Braun - 2</p>
  
  <p>etc.</p>
  
  <p>Currently I get:</p>
  
  <p>Men's Lightweight</p>
  
  <p>Women's Lightweight</p>
  
  <p>It lists the categories, but not the athletes. Where am I going wrong?</p>