Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_authors_by_books_count():
- authors = Author.objects.prefetch_related('book_author', 'book_co_authors')\
- .annotate(
- books_count=Count('book_author'),
- co_books_count=Count('book_co_authors'),
- books_total_count=F('books_count') + F('co_books_count')
- ).order_by('-books_total_count', 'name')[:3]
- if not authors or authors[0].books_total_count == 0:
- return "No results."
- return "\n".join(f"{a.name} authored {a.books_total_count} books." for a in authors)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement