from rest_framework import status
from rest_framework.response import Response
from rest_framework.exceptions import ValidationError, NotFound
from rest_framework.views import APIView
class OrderDetailView(APIView):
def get(self, request, order_id):
try:
order = Order.objects.get(id=order_id)
except Order.DoesNotExist:
raise NotFound(detail="Order not found")
serializer = OrderSerializer(order)
return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request):
serializer = OrderSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)Status Codes and Exception Handling in DRF
NotFound raises HTTP 404, successful GET returns 200, successful POST creation returns 201, validation errors return 400.
NotFound raises HTTP 403 Forbidden because the order is inaccessible to the user.
The POST endpoint should return HTTP 200 instead of 201 because the response includes the serialized data.
serializer.errors is automatically converted to JSON by DRF, but the status code should be HTTP 422 Unprocessable Entity, not 400.
Show answer & explanationAnswer
Correct answer
NotFound raises HTTP 404, successful GET returns 200, successful POST creation returns 201, validation errors return 400.
Explanation
DRF exception classes (NotFound, ValidationError) automatically map to appropriate HTTP status codes. HTTP_201_CREATED indicates successful resource creation.
Written by ExamHoot EditorialPublished · Updated
All 29 Django REST API Development questions
- 1.What is the primary purpose of Django REST Framework (DRF)?
- 2.What is a Serializer in Django REST Framework?
- 3.Which decorator is used to convert a regular Django view into an API view in DRF?
- 4.What is the purpose of ViewSets in Django REST Framework?
- 5.Which class should be inherited to create a basic API view in DRF?
- 6.What is the default authentication method in Django REST Framework?
- 7.How do you register URLs for ViewSets in Django REST Framework?
- 8.What does the status module in DRF provide?
- 9.Which permission class allows only authenticated users to access an API endpoint?
- 10.What is the Response class used for in Django REST Framework?
- 11.Scenario:
- 12.What will be the output of the following code snippet?
- 13.Scenario:
- 14.What is the difference between ModelSerializer and Serializer in Django REST Framework?
- 15.Identify the bug in the following code snippet:
- 16.How do you implement pagination in a Django REST Framework API?
- 17.Scenario:
- 18.What is the purpose of the queryset attribute in a ViewSet?
- 19.Scenario:
- 20.What will be the output of this code snippet?
- 21.Understanding DRF Serializers and Nested Relationships
- 22.DRF Viewsets and Router Configuration Scenario
- 23.Authentication and Permissions in DRF
- 24.Pagination and Filtering in DRF Scenario
- 25.Custom Validation in DRF Serializers
- 26.Status Codes and Exception Handling in DRF
- 27.Throttling and Rate Limiting Scenario
- 28.Request and Response Cycle with Middleware
- 29.Advanced Serializer Optimization and Performance
