18

Surprising that I could not find a way to make a group by query.

I have a query set qs and I am trying to group by some_prop.val, considering qs is excluding entries where some_prop is None.

Let's say the values are [1, 2, 3], then I would be after a result like this:

{1: entries1, 2: entries2, 3: entries3}

Does the Django ORM provide any feature to group results like this?

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
dabadaba
  • 9,064
  • 21
  • 85
  • 155

1 Answers1

32

There is not a specific Django ORM way (as far as I know) but you can do the following to get a dictionary of entries grouped by the values of a field:

  1. Use .values_list() with flat=True to get a list of the existent values in your database (if you don't know them beforehand). Also, use .distinct() to eliminate duplicate values as we do not care for those:

    value_list = MyModel.objects.values_list(
        'interesting_field', flat=True
    ).distinct()
    
  2. Iterate through value_list and fill your dictionary:

    group_by_value = {}
    for value in value_list:
        group_by_value[value] = MyModel.objects.filter(interesting_field=value)
    

Now, the group_by_value dictionary contains as keys the distinct values in your interesting_field and as values the queryset objects, each containing the entries of MyModel with interesting_field=a value from value_list.




Leaving this here for comment legacy reasons.

I have made a Q&A style example in, which simulates a COUNT ... GROUP BY SQL query.

Essentially you need to utilize the .order_by for grouping and the .annotate() to count on the model's .values().

Here is the above-mentioned example:

We can perform a COUNT ... GROUP BY SQL equivalent query on Django ORM, with the use of annotate(), values(), order_by() and the django.db.models's Count methods:

Let our model be:

class Books(models.Model):
    title  = models.CharField()
    author = models.CharField()

Let's assume that we want to count how many book objects per distinct author exist in our Book table:

result = Books.objects.values('author')
                      .order_by('author')
                      .annotate(count=Count('author'))

Now result contains a queryset with two columns: author and count:

  author    | count
------------|-------  
 OneAuthor  |   5 
OtherAuthor |   2    
   ...      |  ...
John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • 4
    yeah but I do not want to group by count, I said very clearly I wanted to group by **values**, how could I be more clear? I even included the expected output. – dabadaba Jul 18 '17 at 12:56