Pydantic
Pydantic is a Python library primarily used for data validation and settings management using Python type hints. At its core, you define data models by creating classes that inherit from Pydantic's
BaseModel. The fields in these models are defined using standard Python type annotations. This makes your code more readable, easier to understand, and integrates well with linters, IDEs, and static analysis tools.
compatible with sqlAlchemy
pydantic is compatible with sqlAlchemy
convert from sqlAlchemy to pydantic model
if the sqlAlchemy model has constrains and relationships, pydantic model needs to be configured with
Features
- type hinting for validation
- more detailed
Fieldcustomization 2.data coercion: auto convert input data to declared types: if declaredint, received"23", will auto convert to23 - clear
@validatorerror: when the fields are problematic - JSON schema/serialization generation: auto convert from/to JSON
- Settings Management: define settings inherit from
BaseSettings-> allows auto load values from.env/secret, make config file clean