Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since I was a little confused about how grouping in Django 1.1 works I thought I'd elaborate here on how exactly you go about using it. First, to repeat what Michael said:</p> <blockquote> <p>Here, as I just discovered, is how to do this with the Django 1.1 aggregation API:</p> <pre><code>from django.db.models import Count theanswer = Item.objects.values('category').annotate(Count('category')) </code></pre> </blockquote> <p>Note also that you need to <code>from django.db.models import Count</code>!</p> <p>This will select only the categories and then add an annotation called <code>category__count</code>. Depending on the default ordering this may be all you need, <strong>but if the default ordering uses a field other than <code>category</code> this will not work</strong>. The reason for this is that the fields required for ordering are also selected and make each row unique, so you won't get stuff grouped how you want it. One quick way to fix this is to reset the ordering:</p> <pre><code>Item.objects.values('category').annotate(Count('category')).order_by() </code></pre> <p>This should produce exactly the results you want. To set the name of the annotation you can use:</p> <pre><code>...annotate(mycount = Count('category'))... </code></pre> <p>Then you will have an annotation called <code>mycount</code> in the results.</p> <p>Everything else about grouping was very straightforward to me. Be sure to check out the <a href="http://docs.djangoproject.com/en/dev/topics/db/aggregation" rel="noreferrer">Django aggregation API</a> for more detailed info.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload