Speed up performance of nested for loops to get all related format of all Authors' Books
If you would use a debugger or something like a django-debug-toolbar, you'd see that you are actually issuing a lot of queries - to get all authors, for each author to get all the books, for each book to get all the formats.

There is a specific tool in Django to solve this kind of problem of getting the related sets - prefetch_related() and select_related(). Replace:

authors = Author.objects.all()  # 1 query
for author in authors:
    books = author.book_set.all() # 1 query per each author


with:

# only 1 query
authors = Author.objects.prefetch_related('books').all()
for author in authors:
    books = author.books.all()


See also this nice article with related examples:


Solving Performance Problems in the Django ORM


For the multiple "prefetch" levels, please see:


prefetch_related for multiple Levels




You can also select only the columns you actually need - use values_list.