What does get_object_or_404(Product, pk=5) do if no matching product exists?
-
A
Raises Http404 (Not Found)
Correct answer
-
B
Returns the first product instead
-
C
Returns None without any error
-
D
Creates a new product with pk 5
Correct answer
A. Raises Http404 (Not Found)
14 views
Explanation
This shortcut wraps .get() and converts DoesNotExist into a proper HTTP 404 response, which keeps detail views clean.
Reference
Asked in: Python Developer Interviews, NTS
Doubts & discussion
Not sure why the answer is A? Ask here and get an explanation.
Ask a doubt about this MCQ