Skip to content

Instantly share code, notes, and snippets.

@PaulSebalu
Last active March 18, 2022 15:53
Show Gist options
  • Save PaulSebalu/1c10d3ed0e0647ac769ae833232c340c to your computer and use it in GitHub Desktop.
Save PaulSebalu/1c10d3ed0e0647ac769ae833232c340c to your computer and use it in GitHub Desktop.
# Link to repo: https://github.com/PaulSebalu/bookstore
# using the interactive shell: "python manage.py shell"
from django.db.models import Count
from store.models import Author
authors=Author.objects.annotate(Count('book')).order_by('-book__count')
for author in authors:
f"{author.name}. {author.book__count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment