diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0475f8dc4..07ff13885 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,7 @@ Added ^^^^^ - Add binary compression support for `UUIDField` in `MySQL`. (#1458) - Only `Model`, `Tortoise`, `BaseDBAsyncClient`, `__version__`, and `connections` are now exported from `tortoise` +- Add parameter `validators` to `pydantic_model_creator`. (#1471) Fixed ^^^^^ diff --git a/tortoise/contrib/pydantic/creator.py b/tortoise/contrib/pydantic/creator.py index 7ac4d816e..86cfeb5e4 100644 --- a/tortoise/contrib/pydantic/creator.py +++ b/tortoise/contrib/pydantic/creator.py @@ -131,6 +131,7 @@ def pydantic_model_creator( exclude_readonly: bool = False, meta_override: Optional[Type] = None, model_config: Optional[ConfigDict] = None, + validators: Optional[Dict[str, Any]] = None, ) -> Type[PydanticModel]: """ Function to build `Pydantic Model `__ off Tortoise Model. @@ -156,6 +157,7 @@ def pydantic_model_creator( :param exclude_readonly: Build a subset model that excludes any readonly fields :param meta_override: A PydanticMeta class to override model's values. :param model_config: A custom config to use as pydantic config. + :param validators: A dictionary of methods that validate fields. Note: Created pydantic model uses config_class parameter and PydanticMeta's 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]]: model = create_model( _name, __base__=PydanticModel, + __validators__=validators, **properties, ) # Copy the Model docstring over