Skip to content

Commit 0ec208b

Browse files
option to set validators of pydantic model (#1471)
* option to set validators of pydantic model * fix: adjust validators of pydantic_model_creator * update changelog --------- Co-authored-by: Lucas Brahm <[email protected]>
1 parent 76cfc14 commit 0ec208b

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Added
1515
^^^^^
1616
- Add binary compression support for `UUIDField` in `MySQL`. (#1458)
1717
- Only `Model`, `Tortoise`, `BaseDBAsyncClient`, `__version__`, and `connections` are now exported from `tortoise`
18+
- Add parameter `validators` to `pydantic_model_creator`. (#1471)
1819

1920
Fixed
2021
^^^^^

tortoise/contrib/pydantic/creator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def pydantic_model_creator(
131131
exclude_readonly: bool = False,
132132
meta_override: Optional[Type] = None,
133133
model_config: Optional[ConfigDict] = None,
134+
validators: Optional[Dict[str, Any]] = None,
134135
) -> Type[PydanticModel]:
135136
"""
136137
Function to build `Pydantic Model <https://pydantic-docs.helpmanual.io/usage/models/>`__ off Tortoise Model.
@@ -156,6 +157,7 @@ def pydantic_model_creator(
156157
:param exclude_readonly: Build a subset model that excludes any readonly fields
157158
:param meta_override: A PydanticMeta class to override model's values.
158159
:param model_config: A custom config to use as pydantic config.
160+
:param validators: A dictionary of methods that validate fields.
159161
160162
Note: Created pydantic model uses config_class parameter and PydanticMeta's
161163
config_class as its Config class's bases(Only if provided!), but it
@@ -439,6 +441,7 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:
439441
model = create_model(
440442
_name,
441443
__base__=PydanticModel,
444+
__validators__=validators,
442445
**properties,
443446
)
444447
# Copy the Model docstring over

0 commit comments

Comments
 (0)