DRF architecture, Serializers, ModelSerializer, and fields
coreintermediateDRF layers a parallel set of building blocks over Django's own request/view/model — Serializers (translate between Python/model objects and JSON, plus validation), Views/ViewSets (the request-handling layer), and Routers (auto-generate URLs from a ViewSet) — each mirroring a Django concept while adding API-specific behavior. A Serializer is DRF's equivalent of a Form: fields declared explicitly, is_valid() runs validation, .data produces JSON-ready output. ModelSerializer is DRF's equivalent of ModelForm — it auto-generates fields from a model's own fields, inferring type and validators (e.g. a CharField's max_length becomes an automatic length validator) the same way ModelForm does, dramatically cutting boilerplate for the common "expose this model over an API" case.
Think of it as
DRF is deliberately built as a set of Django-shaped analogues rather than an unrelated API framework bolted on — Serializer:Form :: ModelSerializer:ModelForm :: APIView:View is the actual, intended mental mapping, because DRF's own design goal was to feel idiomatic to someone who already knows Django, not to introduce an entirely separate paradigm. This is precisely why ModelSerializer auto-generating fields from a model works so similarly to ModelForm: both walk the model's field definitions and infer the corresponding serializer/form field type plus its validators (max_length, choices, unique, blank/null) automatically, since a model field already carries all the information needed to build a reasonable default. The layered architecture (Serializer for shape/validation, View/ViewSet for request handling, Router for URL generation) exists so each concern can be swapped independently — a ModelViewSet with a plain APIView-style serializer, or a plain APIView with a ModelSerializer, are both legitimate combinations, because DRF doesn't force the layers to be used only in lockstep.
What we're doing: A ModelSerializer exposing an explicit field list, with one field overridden to change its inferred behavior.
- 2
- author_name is not a real model field — source="author.get_full_name" tells DRF to traverse the relationship and call a method, a common ModelSerializer pattern for a computed/derived output field.
- 6
- fields lists author_name (the custom field), not author — an explicit field list can mix real model fields with declared ones freely.
Why this works: Exposing a computed "author_name" instead of a raw author id/nested object is a common API-shaping decision — ModelSerializer supports mixing auto-generated model fields with explicitly declared ones in the same fields list, which is what makes this kind of customization straightforward rather than requiring a full manual Serializer.
Using Meta.fields = "__all__" on a ModelSerializer for a model that will grow new fields later
Wrong
Better
What you see: A newly added model field (e.g. an internal is_flagged_for_review boolean, or worse, a sensitive one) is automatically exposed over the API the moment it's added to the model — with no code change to the serializer itself, and no review step that would have caught it.
Why: "__all__" ties the API's exposed shape directly to the model's current field set, meaning any future model change silently changes the API contract too — an explicit field list requires a deliberate, reviewable code change to expose anything new, which is exactly the safety property "__all__" gives up for convenience.
- Django
- Form / ModelForm
- View
- DRF
- Serializer / ModelSerializer
- APIView / ViewSet
- Router
DRF's Django-shaped analogues
Together
Remember: DRF mirrors Django's own shapes: Serializer:Form, ModelSerializer:ModelForm, APIView/ViewSet:View. ModelSerializer infers field type and validators from the model, the same way ModelForm does — an explicit field list (never "__all__") keeps the API surface a deliberate, reviewable decision rather than tied automatically to the model's current fields. Always call is_valid() before reading .data/.validated_data on an input serializer.
See also: serializer validation · nested and context aware serializers · instance methods and domain logic

