PythiloLesson 9 of 20 · progress kept in this browser

Validate nested collections

A boundary often contains a collection of structured records. Nesting models lets Pydantic validate both the outer object and every inner item, while error locations retain the path through the collection.

After validation, the inner values are model instances rather than unchecked dictionaries. Business logic can therefore use attributes and trust the declared constraints. Dumping the outer model recursively serializes those inner models. The input data remains the caller's object; avoid using it as a second, unvalidated source for later calculations.

from pydantic import BaseModel
class Item(BaseModel):
    units: int
class Order(BaseModel):
    items: list[Item]
order = Order(items=[{"units": "3"}])
print(order.items[0].units)
Your task

Implement order_total(raw) validating Order(items: list[Line]), where Line has price: float >=0 and quantity: int >0. Return the sum of price times quantity; an empty order costs zero.

The first run loads pydantic into this browser.