Sample questions
Question 1Easy
Which method returns all rows of a model as a QuerySet?
- Model.objects.list()
- Model.all()
- Model.objects.all()
- Model.objects.select()
Answer: C. Model.objects.all()
Explanation
QuerySets are lazy; they hit the database when evaluated.
Step-by-step solution
- Call all.
- Iterate to evaluate.
Why the other options are wrong
- A.
Model.objects.list() is not a real method; the correct choice is Model.objects.all(). - B. all lives on the manager.
- D.
Model.objects.select() is not a real method; the correct choice is Model.objects.all().
Question 2Medium
What is the output type of Post.objects.filter(a=1)?
- A Python list holding the matching model objects, loaded immediately from the database
- A single object
- None if nothing found
- A QuerySet, which may be empty
Answer: D. A QuerySet, which may be empty
Explanation
filter always returns a QuerySet; get returns one object or raises.
Step-by-step solution
- filter returns QuerySet.
- get returns an object.
Why the other options are wrong
- A. It is lazy, not a list.
- B. get returns one object.
- C. It returns an empty queryset.
Question 3Medium
What does aggregate(Avg('price')) return?
- A QuerySet with one row per object, each carrying the average as an attribute
- A float directly
- A dict such as {'price__avg': 12.5}
- A list of tuples
Answer: C. A dict such as {'price__avg': 12.5}
Explanation
aggregate computes over the whole queryset; annotate adds a value per row.
Step-by-step solution
- Aggregate collapses.
- Dict key is price__avg.
Why the other options are wrong
- A. It collapses to one result.
- B. It returns a dict.
- D. aggregate returns a dict keyed like price__avg, not a list of tuples.
Question 4Medium
What does get() raise if multiple objects match?
- MultipleObjectsReturned
- DoesNotExist
- ValueError
- IndexError
Answer: A. MultipleObjectsReturned
Explanation
get expects exactly one row.
Step-by-step solution
- Zero gives DoesNotExist.
- Many give MultipleObjectsReturned.
Why the other options are wrong
- B. That is for zero matches.
- C. Not raised.
- D. Not raised.
Question 5Medium
Which lookup finds titles containing 'py' ignoring case?
- title__like='py'
- title__icontains='py'
- title__contains_i='py'
- title__regex='py' only
Answer: B. title__icontains='py'
Explanation
The double underscore separates field and lookup type.
Step-by-step solution
- Field then lookup.
- i means ignore case.
Why the other options are wrong
- A. No like lookup.
- C. contains_i is not a lookup; the case-insensitive form is spelled icontains.
- D. Regex is case sensitive by default and not equivalent.
Question 6Medium
What does Post.objects.exclude(draft=True) return?
- Only the drafts, because exclude keeps the rows that match the condition
- All posts
- Posts where draft is not True
- Nothing
Answer: C. Posts where draft is not True
Explanation
exclude is the inverse of filter.
Step-by-step solution
- Condition matches drafts.
- Those are removed.
Why the other options are wrong
- A. That is filter.
- B. Drafts are excluded.
- D. Not empty.
Question 7Medium
What does update_or_create(defaults=..., **lookup) return?
- Only the saved or updated object, without any flag about what happened to it
- A tuple (object, created)
- True or False
- A QuerySet
Answer: B. A tuple (object, created)
Explanation
created is True when a new row was inserted.
Step-by-step solution
- Lookup row.
- Update or create.
Why the other options are wrong
- A. It returns two values.
- C. Created flag is part of tuple.
- D. update_or_create returns an (object, created) tuple, not a QuerySet.
Question 8Medium
What does Post.objects.order_by('-created') do?
- Sorts by created ascending, so the oldest row appears first in the results
- Sorts by created descending
- Removes the field
- Groups by created
Answer: B. Sorts by created descending
Explanation
A leading minus reverses the order.
Step-by-step solution
- Add minus.
- Newest first.
Why the other options are wrong
- A. Minus means descending.
- C. Nothing is removed.
- D. No grouping.
Question 9Hard
Which expression filters Posts where likes is greater than comments (two columns)?
- Post.objects.filter(likes__gt=F('comments'))
- filter(likes__gt='comments')
- filter(likes > comments)
- filter(likes=Q('comments'))
Answer: A. Post.objects.filter(likes__gt=F('comments'))
Explanation
F() references another column inside the database.
Step-by-step solution
- Use F.
- Compare columns.
Why the other options are wrong
- B. Compares against a literal string.
- C. Not valid Python.
- D. Q combines conditions, not fields.
Question 10Hard
How many queries does this run?
for p in Post.objects.all():
print(p.author.name)
(Post has FK author, 10 posts)
- 1, because all authors are fetched together with the posts in the first query
- 2
- 10
- 11 (one for posts and one per author)
Answer: D. 11 (one for posts and one per author)
Explanation
This is the N+1 problem; select_related('author') reduces it to one query.
Step-by-step solution
- 1 query for posts.
- 1 per p.author access.
Why the other options are wrong
- A. Authors are fetched lazily.
- B. Not a join.
- C. The first query is also run.
Practise all 16 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →