From Flat Models to Production Architecture — Advanced Models Review
A week ago, your models were flat. One level. One entity. A customer with a name and email. A payment with an amount and currency. Clean, simple, and completely disconnected from the data you actually work with.
Then you nested your first model.
Remember that moment? You put an Address inside a Customer, passed in a nested dict, and Pydantic validated the entire tree — inner model first, then outer. One line of code, recursive validation. The kind of thing that used to take a dozen isinstance checks and nested if statements.
By mid-week, you weren't just nesting — you were configuring. ConfigDict with extra='forbid' turned your permissive models into strict contracts. Frozen models became immutable records that nobody could accidentally mutate after creation. You learned that model configuration isn't about preferences — it's about declaring intent.
Then serialization clicked. model_dump() with include, exclude, by_alias — you realized that the same data can wear different faces. An internal representation for your business logic. A public representation for your API. A database representation for your ORM. Same model, different views, zero duplication.
And then the surprise: model_json_schema(). Your Pydantic models generate JSON Schema automatically. Documentation that can't drift out of date because it's generated from the same code that validates the data. Every API tool — Swagger, Postman, OpenAPI — speaks JSON Schema. You just got free documentation.
But here's the question that should be nagging you: what about data that changes shape? A webhook that's a Payment one minute and a Refund the next? Your nested models are powerful, but they assume you know the shape in advance. Next week, you'll learn what to do when you don't.
Practice your skills
Sign up to write and run code in this lesson.
From Flat Models to Production Architecture — Advanced Models Review
A week ago, your models were flat. One level. One entity. A customer with a name and email. A payment with an amount and currency. Clean, simple, and completely disconnected from the data you actually work with.
Then you nested your first model.
Remember that moment? You put an Address inside a Customer, passed in a nested dict, and Pydantic validated the entire tree — inner model first, then outer. One line of code, recursive validation. The kind of thing that used to take a dozen isinstance checks and nested if statements.
By mid-week, you weren't just nesting — you were configuring. ConfigDict with extra='forbid' turned your permissive models into strict contracts. Frozen models became immutable records that nobody could accidentally mutate after creation. You learned that model configuration isn't about preferences — it's about declaring intent.
Then serialization clicked. model_dump() with include, exclude, by_alias — you realized that the same data can wear different faces. An internal representation for your business logic. A public representation for your API. A database representation for your ORM. Same model, different views, zero duplication.
And then the surprise: model_json_schema(). Your Pydantic models generate JSON Schema automatically. Documentation that can't drift out of date because it's generated from the same code that validates the data. Every API tool — Swagger, Postman, OpenAPI — speaks JSON Schema. You just got free documentation.
But here's the question that should be nagging you: what about data that changes shape? A webhook that's a Payment one minute and a Refund the next? Your nested models are powerful, but they assume you know the shape in advance. Next week, you'll learn what to do when you don't.
Practice your skills
Sign up to write and run code in this lesson.