The Authors Page
Part of the Models and the Admin section of Coddy's Django journey. Lesson 22 of 23.
Your bookshelf has a page per book but none for authors. This project builds one from what you know: a view that queries the database, a URL, a template that follows relations, and a QuerySet that does not run a query per row.
Each author is listed with their books: a loop over the authors, and inside it a loop over each author's books. Without help the inner loop runs one query per author. select_related() cannot help, because from an author to their books is the way back. Its partner, prefetch_related(), fetches the related rows of every object in the list with one more query.
The task says what the page must do; the code is yours.
Challenge
MediumBuild an Authors page at /authors/, URL name authors:
- Each author gets a
<section class="author">with their name in an<h2>and links to each of their books. - The page runs the same few queries however many authors there are.
- The header in
books/base.htmllinks to it asAuthors.
Press Run and click Authors.
Try it yourself
from django.shortcuts import get_object_or_404, render
from .models import Book
def home(request):
books = Book.objects.select_related("author")
return render(request, "books/home.html", {"books": books})
def book_detail(request, pk):
book = get_object_or_404(Book, pk=pk)
return render(request, "books/book_detail.html", {"book": book})
def search(request):
q = request.GET.get("q", "")
books = Book.objects.filter(title__icontains=q).select_related("author")
return render(request, "books/search.html", {"q": q, "books": books})
All lessons in Models and the Admin
Practice on your own: Django playground