liveBook · Manning

Pydantic

As explained in Django in Action

Pydantic is a Python library for declaring data schemas with type annotations and validating or serializing structured data.

Pydantic declares structured data with Python type hints, then helps Django Ninja serialize that data for API responses. Its class-based model resembles dataclasses, while remaining distinct from Django ORM models. section 11.2.1

For database objects, Ninja’s ModelSchema connects a Pydantic-style schema to a Django model. Choose the model and output fields, return the queryset, and let the API produce JSON. section 11.2.1

Serialize promoters

Define PromoterSchema with model=Promoter and fields for id, full_name, birth, and death. Declare the endpoint response as list[PromoterSchema], return Promoter.objects.all(), and receive a JSON list with dates and null for a missing death date.

How Django in Action covers it

How deep it goes
Explains typed schema declarations and ModelSchema configuration, stopping short of Pydantic internals.
Examples
Uses a promoters endpoint that serializes identifiers, names, dates, and a null death value.
What it assumes
Assumes Python classes, type hints, Django models, and Django Ninja routing, while contrasting schemas with ORM models.
What you can do after
Build it from scratch by defining a ModelSchema and returning a Django queryset from a typed API response.
How it connects
Connects Python type hints, dataclasses, Django ORM models, Django Ninja, and JSON serialization.

Also explained in

Compare how 4 books explain Pydantic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.