title: Django ORM & hstore : counting unique values of a key
body: <p>Having the following model:</p>
  
  <pre><code>from django_hstore import hstore
  from django.db import models
  
  class Item(VoteModel):
      data = hstore.DictionaryField(db_index=True)
      objects = hstore.HStoreManager()
  </code></pre>
  
  <p>Something like:</p>
  
  <pre><code>Item.objects.extra(select={"key": "content_item.data -&gt; 'key'"}).aggregate(Count('key'))
  </code></pre>
  
  <p>doesn't work, cf. <a href="https://stackoverflow.com/q/4567543/343834">Using .aggregate() on a value introduced using .extra(select={...}) in a Django Query?</a> and <a href="https://code.djangoproject.com/ticket/11671" rel="nofollow noreferrer">https://code.djangoproject.com/ticket/11671</a>.</p>
  
  <p>The raw SQL which works is the following:</p>
  
  <pre><code>SELECT content_item.data -&gt; 'key' AS key, count(*)  FROM content_item GROUP BY key;                                                                               
     key     | count 
  -----------+-------
   value1    |   223
   value2    |    28
   value3    |    31
  (3 rows)
  </code></pre>
  
  <p>How can I get hte same results through Django's ORM ?</p>
  
  <p>FYI:</p>
  
  <pre><code>Item.objects.extra(select={"key": "content_item.data -&gt; 'key'"})
  </code></pre>
  
  <p>translates to:</p>
  
  <pre><code>SELECT (content_item.data -&gt; 'key') AS "key", "content_item"."id", "content_item"."data" FROM "content_item"
  </code></pre>