mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
82ef45c890
* fix iteration to not convert to dict by default * add change * remove extra newline
30 lines
800 B
Python
30 lines
800 B
Python
from datetime import datetime, timedelta
|
|
from pydantic import BaseModel
|
|
from pydantic.json import timedelta_isoformat
|
|
|
|
class BarModel(BaseModel):
|
|
whatever: int
|
|
|
|
class FooBarModel(BaseModel):
|
|
foo: datetime
|
|
bar: BarModel
|
|
|
|
m = FooBarModel(foo=datetime(2032, 6, 1, 12, 13, 14), bar={'whatever': 123})
|
|
print(m.json())
|
|
# (returns a str)
|
|
# > {"foo": "2032-06-01T12:13:14", "bar": {"whatever": 123}}
|
|
|
|
class WithCustomEncoders(BaseModel):
|
|
dt: datetime
|
|
diff: timedelta
|
|
|
|
class Config:
|
|
json_encoders = {
|
|
datetime: lambda v: (v - datetime(1970, 1, 1)).total_seconds(),
|
|
timedelta: timedelta_isoformat,
|
|
}
|
|
|
|
m = WithCustomEncoders(dt=datetime(2032, 6, 1), diff=timedelta(hours=100))
|
|
print(m.json())
|
|
# > {"dt": 1969660800.0, "diff": "P4DT4H0M0.000000S"}
|