title: Count number of instances which have a parameter in a list in common
body: <p>I'm building an app to keep track of a school and I'm trying to figure out how many students each course has. The way my app is structured is like this:</p>
  <pre><code>class Course(models.Model):
      LEVELS = (
          ('A0', 'A0'),
          ('A1', 'A1'),
          ('B0', 'B0'),
          ('B1', 'B1'),
          ('C1', 'C1'),
          ('C2', 'C2'),
      )
      name = models.CharField(max_length=32)
      level = models.CharField(max_length=2, choices=LEVELS)
  
  class Student(models.Model):
      name = models.CharField(max_length=32)
      school = models.ForeignKey(School, on_delete=models.CASCADE)
      course = models.ManyToManyField(Course)
  </code></pre>
  <p>So, the students in the API look something like this:</p>
  <pre><code>[
  {
          &quot;id&quot;: 1,
          &quot;name&quot;: &quot;Diego&quot;,
          &quot;school&quot;: 2,
          &quot;course&quot;: [8, 11]
      },
      {
          &quot;id&quot;: 2,
          &quot;name&quot;: &quot;Garry&quot;,
          &quot;school&quot;: 2,
          &quot;course&quot;: [4]
      },
      {
          &quot;id&quot;: 3,
          &quot;name&quot;: &quot;Dr Henrick&quot;,
          &quot;school&quot;: 2,
          &quot;course&quot;: [8]
      },
  ]
  </code></pre>
  <p>Now the question comes in my serializer, where I'd like to get a count for each course, ideally in a dict structure that would look something like this:</p>
  <pre><code>{ 8: 2, 11: 1, 4: 1}
  </code></pre>
  <p>What I've done consists of getting all of the students from the db and looping over their courses list, but I wonder if I could do something like that with Django's query system.</p>