# views.py
def books_list(self):
books_name = {}
total = 0
for item in Author.objects.all().order_by('books__name'):
first_book = item.books.first()
if first_book:
total += 1
books_name[first_book] = books_name.get(first_book, 0) + 1
return {'books_name': books_name, 'total_books': total}
# template.html
{% for book, quant in books_list.books_name.items %}
<tbody>
<tr>
<td>{{ book }}</td>
<td>{{ quant }}</td>
</tr>
</tbody>
{% endfor %}