mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
f55515820a
* renaming .json -> .model_dump_json * renaming .dict -> .model_dump * renaming .__fields__ -> .model_fields * renaming .schema -> .model_json_schema * renaming .construct -> .model_construct * renaming .parse_obj -> .model_validate * make linters happy * add changes md-file Co-authored-by: Samuel Colvin <s@muelcolvin.com>
19 lines
444 B
Python
19 lines
444 B
Python
from datetime import datetime, timedelta
|
|
from pydantic import BaseModel
|
|
from pydantic.json import timedelta_isoformat
|
|
|
|
|
|
class WithCustomEncoders(BaseModel):
|
|
dt: datetime
|
|
diff: timedelta
|
|
|
|
class Config:
|
|
json_encoders = {
|
|
datetime: lambda v: v.timestamp(),
|
|
timedelta: timedelta_isoformat,
|
|
}
|
|
|
|
|
|
m = WithCustomEncoders(dt=datetime(2032, 6, 1), diff=timedelta(hours=100))
|
|
print(m.model_dump_json())
|