Categories
Python Answers

How to query as GROUP BY in Python Django?

Spread the love

To query as GROUP BY in Python Django, we can use the aggregation features in the Django ORM.

For instance, we write

from django.db.models import Count

result = (Members.objects
    .values('designation')
    .annotate(dcount=Count('designation'))
    .order_by()
)

to get the designation column values with values.

Then we get the count of the designation column values with Count.

And then we call order_by sort the values in ascending order.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *