0
0
Djangoframework~20 mins

exclude() for negation in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exclude Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Django queryset return?
Given a model Book with a field genre, what does this queryset return?

Book.objects.exclude(genre='fiction')
AAll books except those with genre exactly 'fiction'.
BOnly books with genre 'fiction'.
CAll books including those with genre 'fiction'.
DBooks with genre not set (null) only.
Attempts:
2 left
💡 Hint
Think about what exclude() does compared to filter().
📝 Syntax
intermediate
2:00remaining
Which queryset correctly excludes books published before 2000?
You want to exclude books published before the year 2000. Which queryset is correct?
ABook.objects.exclude(published_year__lt=2000)
BBook.objects.exclude(published_year<2000)
CBook.objects.filter(~Q(published_year__lt=2000))
DBook.objects.exclude(published_year__before=2000)
Attempts:
2 left
💡 Hint
Look for the correct Django field lookup syntax.
🔧 Debug
advanced
2:00remaining
Why does this exclude() queryset raise an error?
Consider this queryset:
Book.objects.exclude(genre='fiction', published_year__lt=2000)

Why might this raise an error or not behave as expected?
AIt excludes books that are fiction OR published before 2000, which is correct.
Bexclude() does not accept multiple keyword arguments.
CThe syntax is invalid because exclude() requires Q objects for multiple conditions.
DIt excludes books that are fiction AND published before 2000, but you wanted to exclude either condition.
Attempts:
2 left
💡 Hint
Think about how multiple arguments in exclude() combine conditions.
🧠 Conceptual
advanced
2:00remaining
How does exclude() differ from filter() in Django querysets?
Which statement best describes the difference between filter() and exclude()?
Aexclude() returns records matching the condition; filter() returns records not matching the condition.
Bfilter() and exclude() both return matching records but exclude() is faster.
Cfilter() returns records matching the condition; exclude() returns records not matching the condition.
Dfilter() returns all records; exclude() returns no records.
Attempts:
2 left
💡 Hint
Think about the meaning of 'exclude' in English.
state_output
expert
2:00remaining
What is the count of books returned by this queryset?
Assume the database has 10 books: 4 fiction, 3 non-fiction, and 3 with genre null.

What is the result of:
Book.objects.exclude(genre='fiction').count()
A3
B6
C7
D10
Attempts:
2 left
💡 Hint
exclude() removes only books with genre exactly 'fiction'. Null genres are included.