Deep dive into pydantic BaseModel class decorators
· 9 min read
Pydantic BaseModel
class decorators are a powerful and modern way to customize a model's behavior and validation logic. While a lot of Pydantic's functionality is configured through class attributes or the ConfigDict
, decorators offer a more explicit and code-centric approach, especially for complex validation.
Pydantic V2 introduced several new decorators to enhance validation and model configuration. The most important ones are:
@model_validator
@field_validator
@computed_field
These decorators are typically imported from pydantic
.