title: Filter model on filtered reverse_set length in Django
body: <p>I have models:</p>
  
  <pre><code>class A(models.Model):
      b = models.ForeignKey(B)
  
  class B(models.Model):
      pass
  
  class C(models.Model):
      b = models.ForeignKey(B)
      bar = models.IntegerField()  # Or any other field
  
  class C(models.Model):
  </code></pre>
  
  <p>I need to filter A by length of c_set of b filtered by value of C.bar. Now I have:</p>
  
  <pre><code>for a in A.objects.all():
      if len(a.b.c_set.filter(bar=1234)) == 0:
          continue
      # Code for needed 'a'
  </code></pre>
  
  <p>How can I filter 'A' with only one database request? I have other filters for 'A' and I need to combine them with new one. Something like this:</p>
  
  <pre><code>A.objects.annotate(newfilter=Count(b__c_set__bar=1234)).filter(**{
      'newfilter__gt': 0,
      # My filters
  })
  </code></pre>