From 8236c2154a4ff4ba35f71bc1d39921683340c47a Mon Sep 17 00:00:00 2001 From: Arthur Pastel Date: Sun, 8 Nov 2020 04:14:37 +0100 Subject: [PATCH] Add more config options inherited from Pydantic --- docs/modeling.md | 73 ++++++++++++++++++++++++++++------------------ odmantic/config.py | 10 +++++-- 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/docs/modeling.md b/docs/modeling.md index f9c63983..7a5bcfa0 100644 --- a/docs/modeling.md +++ b/docs/modeling.md @@ -58,38 +58,53 @@ ensure that the area of the rectangle is less or equal to 9. ### Advanced Configuration -The model configuration is done in the same way as with Pydantic models, using a [Config +The model configuration is done in the same way as with Pydantic models: using a [Config class](https://pydantic-docs.helpmanual.io/usage/model_config/){:target=blank_} defined in the model body. - - -`#!python collection: str` -: The collection name associated to the model. see [this - section](modeling.md#collection) for more details about collection naming. - - - -`#!python title: str` *(inherited from Pydantic)* -: The name generated in the JSON schema. - - - -`#!python json_encoders: dict` *(inherited from Pydantic)* -: Customize the way types used in the model are encoded. - - ??? example "`json_encoders` example" - - For example, in order to serialize `datetime` fields as timestamp values: - ```python - class Event(Model): - date: datetime - - class Config: - json_encoders = { - datetime: lambda v: v.timestamp() - } - ``` +**Available options**: + +> `#!python collection: str` +> : Customize the collection name associated to the model. see [this +> section](modeling.md#collection) for more details about collection naming. +> +> `#!python title: str` *(inherited from Pydantic)* +> : Title inferred in the JSON schema. +> +> `#!python anystr_strip_whitespace: bool` *(inherited from Pydantic)* +> : Whether to strip leading and trailing whitespaces for str & byte types. +> +> Default: `#!python False` +> +> `#!python json_encoders: dict` *(inherited from Pydantic)* +> : Customize the way types used in the model are encoded to JSON. +> +> ??? example "`json_encoders` example" +> +> For example, in order to serialize `datetime` fields as timestamp values: +> ```python +> class Event(Model): +> date: datetime +> +> class Config: +> json_encoders = { +> datetime: lambda v: v.timestamp() +> } +> ``` +> +> `#!python json_loads` *(inherited from Pydantic)* +> : Function used to decode JSON data +> +> Default: `#!python json.loads` +> +> `#!python json_dumps` *(inherited from Pydantic)* +> : Function used to encode JSON data +> +> Default: `#!python json.dumps` + +For more details and examples about the options inherited from Pydantic, you can have a +look to [Pydantic: Model +Config](https://pydantic-docs.helpmanual.io/usage/model_config/){:target=blank_} !!! warning Only the options described above are supported and other options from Pydantic can't diff --git a/odmantic/config.py b/odmantic/config.py index 50a3948c..c0433ddc 100644 --- a/odmantic/config.py +++ b/odmantic/config.py @@ -1,4 +1,5 @@ -from typing import Any, Dict, Optional, Type +import json +from typing import Any, Callable, Dict, Optional, Type from pydantic.main import BaseConfig from pydantic.typing import AnyCallable @@ -8,13 +9,18 @@ class BaseODMConfig: - """Base class of the Config defined in the Models""" + """Base class of the Config defined in the Models + Defines as well the fields allowed to be passed. + """ collection: Optional[str] = None # Inherited from pydantic title: Optional[str] = None json_encoders: Dict[Type[Any], AnyCallable] = {} + anystr_strip_whitespace: bool = False + json_loads: Callable[[str], Any] = json.loads + json_dumps: Callable[..., str] = json.dumps ALLOWED_CONFIG_OPTIONS = {name for name in dir(BaseODMConfig) if not is_dunder(name)}