使用 pydantic json_decoders 解析不同的时间格式未按预期工作
Parsing different time formats with pydantic json_decoders not working as expected
有人可以向我解释以下 pydantic 模型的行为吗?
from datetime import datetime
from pydantic import BaseModel
first_format = {'time': '2018-01-05T16:59:33+00:00',}
second_format = {'time': '2021-03-05T08:21:00.000Z',}
class TimeModel(BaseModel):
time: datetime
class Config:
json_encoders = {
datetime: lambda v: v.isoformat(),
}
json_decoders = {
datetime: lambda v: datetime.fromisoformat(v),
}
print(TimeModel.parse_obj(first_format))
print("first_format successfull")
print(TimeModel.parse_obj(second_format))
print("second_format successfull")
输出:
time=datetime.datetime(2018, 1, 5, 16, 59, 33, tzinfo=datetime.timezone.utc)
first_format successfull
time=datetime.datetime(2021, 3, 5, 8, 21, tzinfo=datetime.timezone.utc)
second_format successfull
预期行为:
像现在一样在 first_format
上工作,但在 second_format
上失败,因为 datetime.fromisoformat(second_format)
会因错误的格式引发错误
我想做的事情:
只有格式为first_format
的时间才解析,否则抛出异常
感谢您的帮助
编辑:
后来才知道根本就没有json_decoders
,大家不要被这个
搞糊涂了
问题已解决
您需要在此处使用 pre
validator
,以便解析和验证传入的日期时间字符串:
from datetime import datetime
from pydantic import BaseModel, validator
first_format = {'time': '2018-01-05T16:59:33+00:00',}
second_format = {'time': '2021-03-05T08:21:00.000Z',}
class TimeModel(BaseModel):
time: datetime
class Config:
json_encoders = {
datetime: lambda v: v.isoformat(),
}
@validator('time', pre=True)
def time_validate(cls, v):
return datetime.fromisoformat(v)
print(TimeModel.parse_obj(first_format).json())
print("first_format successfull")
print(TimeModel.parse_obj(second_format))
print("second_format successfull")
有人可以向我解释以下 pydantic 模型的行为吗?
from datetime import datetime
from pydantic import BaseModel
first_format = {'time': '2018-01-05T16:59:33+00:00',}
second_format = {'time': '2021-03-05T08:21:00.000Z',}
class TimeModel(BaseModel):
time: datetime
class Config:
json_encoders = {
datetime: lambda v: v.isoformat(),
}
json_decoders = {
datetime: lambda v: datetime.fromisoformat(v),
}
print(TimeModel.parse_obj(first_format))
print("first_format successfull")
print(TimeModel.parse_obj(second_format))
print("second_format successfull")
输出:
time=datetime.datetime(2018, 1, 5, 16, 59, 33, tzinfo=datetime.timezone.utc)
first_format successfull
time=datetime.datetime(2021, 3, 5, 8, 21, tzinfo=datetime.timezone.utc)
second_format successfull
预期行为:
像现在一样在 first_format
上工作,但在 second_format
上失败,因为 datetime.fromisoformat(second_format)
会因错误的格式引发错误
我想做的事情:
只有格式为first_format
的时间才解析,否则抛出异常
感谢您的帮助
编辑:
后来才知道根本就没有json_decoders
,大家不要被这个
问题已解决
您需要在此处使用 pre
validator
,以便解析和验证传入的日期时间字符串:
from datetime import datetime
from pydantic import BaseModel, validator
first_format = {'time': '2018-01-05T16:59:33+00:00',}
second_format = {'time': '2021-03-05T08:21:00.000Z',}
class TimeModel(BaseModel):
time: datetime
class Config:
json_encoders = {
datetime: lambda v: v.isoformat(),
}
@validator('time', pre=True)
def time_validate(cls, v):
return datetime.fromisoformat(v)
print(TimeModel.parse_obj(first_format).json())
print("first_format successfull")
print(TimeModel.parse_obj(second_format))
print("second_format successfull")