title: How to annotate Count with a condition in a Django queryset
body: <p>Using Django ORM, can one do something like <code>queryset.objects.annotate(Count('queryset_objects', gte=VALUE))</code>. Catch my drift? </p>
  
  <hr>
  
  <p>Here's a quick example to use for illustrating a possible answer:</p>
  
  <p>In a Django website, content creators submit articles, and regular users view (i.e. read) the said articles. Articles can either be published (i.e. available for all to read), or in draft mode. The models depicting these requirements are:</p>
  
  <pre><code>class Article(models.Model):
      author = models.ForeignKey(User)
      published = models.BooleanField(default=False)
  
  class Readership(models.Model):
      reader = models.ForeignKey(User)
      which_article = models.ForeignKey(Article)
      what_time = models.DateTimeField(auto_now_add=True)
  </code></pre>
  
  <p><strong>My question is:</strong> How can I get all published articles, sorted by unique readership from the last 30 mins? I.e. I want to count how many distinct (unique) views each published article got in the last half an hour, and then produce a list of articles sorted by these distinct views.</p>
  
  <hr>
  
  <p>I tried:</p>
  
  <pre><code>date = datetime.now()-timedelta(minutes=30)
  articles = Article.objects.filter(published=True).extra(select = {
    "views" : """
    SELECT COUNT(*)
    FROM myapp_readership
      JOIN myapp_article on myapp_readership.which_article_id = myapp_article.id
    WHERE myapp_readership.reader_id = myapp_user.id
    AND myapp_readership.what_time &gt; %s """ % date,
  }).order_by("-views")
  </code></pre>
  
  <p>This sprang the error: <strong>syntax error at or near "01"</strong> (where "01" was the datetime object inside extra). It's not much to go on.</p>