Skip to content

Commit

Permalink
Add more config options inherited from Pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Nov 8, 2020
1 parent 2d7266b commit 8236c21
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
73 changes: 44 additions & 29 deletions docs/modeling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions odmantic/config.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)}
Expand Down

0 comments on commit 8236c21

Please sign in to comment.