Django REST Framework (DRF) is the standard for JSON APIs—serializers, viewsets, routers, and browsable API docs on top of Django.
Core concepts
- Serializer — converts models ↔ JSON, validates input like forms
- APIView / ViewSet — class-based API endpoints
- Router — auto URL patterns for viewsets (
/articles/,/articles/5/) - Permission classes — IsAuthenticated, IsAdminUser, custom rules
When to add DRF
Server-rendered Django templates for SEO and admin; DRF when React/Vue/mobile clients consume JSON. Same models and auth—different presentation layer.
Important interview questions and answers
- Q: Serializer vs ModelForm?
A: Both validate input—serializers output JSON for APIs; forms output HTML fields. - Q: ViewSet vs APIView?
A: ViewSet groups list/create/retrieve/update/destroy; APIView is one class per endpoint style. - Q: Pagination?
A: DRF paginators limit list payload size—cursor or page number pagination for large tables.
Self-check
- What does a serializer replace in an API?
- When would you add DRF to a Django project?
Tip: Serializers whitelist fields like ModelForms—never expose write-only sensitive columns (e.g. is_superuser) on create endpoints.
Interview prep
- Serializer vs ModelForm?
Both validate input—serializers emit JSON for APIs; ModelForms render HTML fields and save to models.