django queryset group by count

 from django.db.models import Count

 result = Books.objects.values('author')

Here is what the above code is Doing:
1. We’re using the .values() method to return a list of dictionaries.
2. We’re ordering the results by the author field.
3. We’re using the .annotate() method to count the number of books for each author.
4. We’re using the .order_by() method to order the results by the count field.

The result of the above code is a list of dictionaries that looks like this:

[{‘author’: ‘A. A. Milne’, ‘count’: 1},
{‘author’: ‘Aldous Huxley’, ‘count’: 1},
{‘author’: ‘C. S. Lewis’, ‘count’: 1},
{‘author’: ‘Dr. Seuss’, ‘count’: 1},
{‘author’: ‘George Orwell’, ‘count’: 1},
{‘author’: ‘J. D. Salinger’, ‘count’: 1},
{‘author’: ‘J. K. Rowling’, ‘count’: 1},
{‘author’: ‘J. R. R. Tolkien’, ‘count’: 1},
{‘author’: ‘Mark Twain’, ‘count’: 1},
{‘author’: ‘Roald Dahl’, ‘count’: 1},
{‘author’: ‘Suzanne Collins’, ‘count’: 1},
{‘author’: ‘William Golding’, ‘count’: 1}]