mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
33c5a4dc34
* Implement merged json_encoders inheritance * json_encoders inheritance documentation
25 lines
547 B
Python
25 lines
547 B
Python
from datetime import datetime, timedelta
|
|
from pydantic import BaseModel
|
|
from pydantic.json import timedelta_isoformat
|
|
|
|
|
|
class BaseClassWithEncoders(BaseModel):
|
|
dt: datetime
|
|
diff: timedelta
|
|
|
|
class Config:
|
|
json_encoders = {
|
|
datetime: lambda v: v.timestamp()
|
|
}
|
|
|
|
|
|
class ChildClassWithEncoders(BaseClassWithEncoders):
|
|
class Config:
|
|
json_encoders = {
|
|
timedelta: timedelta_isoformat
|
|
}
|
|
|
|
|
|
m = ChildClassWithEncoders(dt=datetime(2032, 6, 1), diff=timedelta(hours=100))
|
|
print(m.json())
|