Upgrade No. 8 to Pydantic v2
This commit is contained in:
28
README.md
28
README.md
@@ -416,16 +416,11 @@ Having a controllable global base model allows us to customize all the models wi
|
|||||||
For example, we could have a standard datetime format or add a super method for all subclasses of the base model.
|
For example, we could have a standard datetime format or add a super method for all subclasses of the base model.
|
||||||
```python
|
```python
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Any
|
||||||
from zoneinfo import ZoneInfo
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
import orjson
|
|
||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
from pydantic import BaseModel, root_validator
|
from pydantic import BaseModel, ConfigDict, model_validator
|
||||||
|
|
||||||
|
|
||||||
def orjson_dumps(v, *, default):
|
|
||||||
# orjson.dumps returns bytes, to match standard json.dumps we need to decode
|
|
||||||
return orjson.dumps(v, default=default).decode()
|
|
||||||
|
|
||||||
|
|
||||||
def convert_datetime_to_gmt(dt: datetime) -> str:
|
def convert_datetime_to_gmt(dt: datetime) -> str:
|
||||||
@@ -435,15 +430,15 @@ def convert_datetime_to_gmt(dt: datetime) -> str:
|
|||||||
return dt.strftime("%Y-%m-%dT%H:%M:%S%z")
|
return dt.strftime("%Y-%m-%dT%H:%M:%S%z")
|
||||||
|
|
||||||
|
|
||||||
class ORJSONModel(BaseModel):
|
class CustomModel(BaseModel):
|
||||||
class Config:
|
model_config = ConfigDict(
|
||||||
json_loads = orjson.loads
|
json_encoders={datetime: convert_datetime_to_gmt},
|
||||||
json_dumps = orjson_dumps
|
populate_by_name=True,
|
||||||
json_encoders = {datetime: convert_datetime_to_gmt} # method for customer JSON encoding of datetime fields
|
)
|
||||||
|
|
||||||
@root_validator()
|
@model_validator(mode="before")
|
||||||
def set_null_microseconds(cls, data: dict) -> dict:
|
@classmethod
|
||||||
"""Drops microseconds in all the datetime field values."""
|
def set_null_microseconds(cls, data: dict[str, Any]) -> dict[str, Any]:
|
||||||
datetime_fields = {
|
datetime_fields = {
|
||||||
k: v.replace(microsecond=0)
|
k: v.replace(microsecond=0)
|
||||||
for k, v in data.items()
|
for k, v in data.items()
|
||||||
@@ -454,9 +449,10 @@ class ORJSONModel(BaseModel):
|
|||||||
|
|
||||||
def serializable_dict(self, **kwargs):
|
def serializable_dict(self, **kwargs):
|
||||||
"""Return a dict which contains only serializable fields."""
|
"""Return a dict which contains only serializable fields."""
|
||||||
default_dict = super().dict(**kwargs)
|
default_dict = self.model_dump()
|
||||||
|
|
||||||
return jsonable_encoder(default_dict)
|
return jsonable_encoder(default_dict)
|
||||||
|
|
||||||
```
|
```
|
||||||
In the example above we have decided to make a global base model which:
|
In the example above we have decided to make a global base model which:
|
||||||
- uses [orjson](https://github.com/ijl/orjson) to serialize data
|
- uses [orjson](https://github.com/ijl/orjson) to serialize data
|
||||||
|
|||||||
Reference in New Issue
Block a user