Challenge - 5 Problems
Select Related Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Understanding select_related effect on query count
Given the Django models
1)
2)
What will be the number of queries executed when iterating over
Author and Book where Book has a foreign key to Author, what is the difference in database queries between these two code snippets?1)
books = Book.objects.all()2)
books = Book.objects.select_related('author').all()What will be the number of queries executed when iterating over
books and accessing book.author.name in each case?Django
for book in books: print(book.author.name)
Attempts:
2 left
💡 Hint
Think about how select_related joins tables to reduce queries.
✗ Incorrect
Without select_related, accessing book.author triggers a separate query per book (N+1 queries). Using select_related fetches related authors in one join query, so only 1 query runs.
📝 Syntax
intermediate2:00remaining
Correct usage of select_related with multiple fields
Which of the following is the correct way to use
select_related to fetch related fields author and publisher in a Django query?Attempts:
2 left
💡 Hint
Check the expected argument type for select_related.
✗ Incorrect
select_related accepts either a single string or a list/tuple of strings for multiple related fields.
🔧 Debug
advanced2:00remaining
Diagnosing unexpected query count with select_related
A developer uses
select_related('author') on a queryset but still sees multiple queries when accessing book.author.profile. Why does this happen?Attempts:
2 left
💡 Hint
Think about how deep select_related fetches related objects.
✗ Incorrect
select_related fetches related objects only one level deep unless nested relations are specified with double underscores.
🧠 Conceptual
advanced2:00remaining
When to prefer select_related over prefetch_related
Which situation is best suited for using
select_related instead of prefetch_related in Django?Attempts:
2 left
💡 Hint
Consider the type of relationship and how joins work.
✗ Incorrect
select_related uses SQL joins and is efficient for single-valued relationships like foreign keys or one-to-one fields. prefetch_related is better for many-to-many or reverse foreign keys.
❓ state_output
expert2:00remaining
Output of nested select_related query
Given these Django models:
What is the output of the following code snippet?
Assuming the database has 3 books with authors and countries properly set, how many database queries will this code execute?
class Country(models.Model):
name = models.CharField(max_length=100)
class Author(models.Model):
name = models.CharField(max_length=100)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)What is the output of the following code snippet?
books = Book.objects.select_related('author__country').all()
for book in books:
print(f"{book.title} by {book.author.name} from {book.author.country.name}")Assuming the database has 3 books with authors and countries properly set, how many database queries will this code execute?
Django
books = Book.objects.select_related('author__country').all() for book in books: print(f"{book.title} by {book.author.name} from {book.author.country.name}")
Attempts:
2 left
💡 Hint
select_related can follow nested foreign keys with double underscores.
✗ Incorrect
The select_related('author__country') fetches books, authors, and countries in a single SQL join query. So only 1 query runs regardless of number of books.