Skip to content

Instantly share code, notes, and snippets.

@PaulSebalu
Last active March 18, 2022 15:53
Show Gist options
  • Save PaulSebalu/3c8a3a5eb88d1fa80e68942fcce757af to your computer and use it in GitHub Desktop.
Save PaulSebalu/3c8a3a5eb88d1fa80e68942fcce757af 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 Prefetch
from store.models import Author
authors=Author.objects.prefetch_related(Prefetch("book_set")).all()
for author in authors:
f"{author.name}:{author.book_set.all()}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment