0
0
Djangoframework~20 mins

Relationship query patterns in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Relationship Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django ORM query?
Given models Author and Book where Book has a ForeignKey to Author, what does this query return?

Author.objects.filter(book__title__icontains='django').distinct()
AA queryset of authors who have no books with 'django' in the title
BA queryset of books with 'django' in the title
CA queryset of all authors regardless of their books
DA queryset of authors who have at least one book with 'django' in the title, without duplicates
Attempts:
2 left
💡 Hint
Think about how the double underscore syntax works in Django ORM for related fields.
state_output
intermediate
2:00remaining
How many items are in the resulting queryset?
Consider these models:
class Publisher(models.Model): name = models.CharField(max_length=100)
class Book(models.Model): title = models.CharField(max_length=100); publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)

Given 3 publishers and 5 books distributed among them, what is the count of this queryset?

Publisher.objects.filter(book__title__startswith='A').distinct().count()
A5
B3
C2
D0
Attempts:
2 left
💡 Hint
Count publishers who have at least one book starting with 'A'.
📝 Syntax
advanced
2:00remaining
Which option correctly uses Django ORM to get all books with authors named 'Alice'?
Given Book with ForeignKey author to Author, which query is correct?
ABook.objects.filter(author__name='Alice')
BBook.objects.filter(author.name='Alice')
CBook.objects.filter('author__name'='Alice')
DBook.objects.filter(author.name=='Alice')
Attempts:
2 left
💡 Hint
Use double underscores to traverse relationships in filters.
🔧 Debug
advanced
2:00remaining
What error does this Django ORM query raise?
Given models Category and Product where Product has ForeignKey category, what error occurs with this query?

Product.objects.filter(category__name__in='Electronics,Books')
ATypeError: argument of type 'str' is not iterable
BSyntaxError: invalid syntax
CNo error, returns products in categories 'Electronics' or 'Books'
DValueError: invalid lookup 'in' for CharField
Attempts:
2 left
💡 Hint
Check the type expected by the __in lookup.
🧠 Conceptual
expert
2:00remaining
Which option best describes the behavior of select_related in Django ORM?
What does select_related do when querying related models?
AIt fetches related objects lazily when accessed, causing additional queries
BIt performs a SQL join and fetches related objects in the same query to reduce database hits
CIt caches all related objects in memory for the entire application runtime
DIt deletes related objects automatically when the main object is deleted
Attempts:
2 left
💡 Hint
Think about how to optimize queries to avoid multiple database hits.