Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - Django GROUP BY field value

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 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 elimintae duplicate values as we do not care for those:

    value_list = MyModel.objects.values_list(
        'interesting_field', flat=True
    ).distinct()
    
  2. Now 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 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()

Lets 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    
   ...      |  ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...