Python Fast API-MySQL-SQLAlchemy:在Pydantic模型中获取ValidationError“Invalid type”
我对这个框架相当陌生,但我正在为一家小公司开发时间表应用程序API 我遇到的问题是,当我从mysql中选择一个时间值时,我在pydantic模块中遇到了以下错误:Python Fast API-MySQL-SQLAlchemy:在Pydantic模型中获取ValidationError“Invalid type”,python,mysql,fastapi,pydantic,Python,Mysql,Fastapi,Pydantic,我对这个框架相当陌生,但我正在为一家小公司开发时间表应用程序API 我遇到的问题是,当我从mysql中选择一个时间值时,我在pydantic模块中遇到了以下错误: pydantic.error_wrappers.ValidationError: 2 validation errors for TimeSheetRange response -> 0 -> total invalid type; expected time, string, bytes, int or floa
pydantic.error_wrappers.ValidationError: 2 validation errors for TimeSheetRange
response -> 0 -> total
invalid type; expected time, string, bytes, int or float (type=type_error)
response -> 1 -> total
invalid type; expected time, string, bytes, int or float (type=type_error)
我已经尝试了很多解决方法来获取从mysql到to_char的字符串时间,但也失败了,但是我更想知道这里的问题是什么,因为我想使用数据类型time
schemas.py
输入导入列表,可选
从日期时间导入日期、时间、日期时间
从pydantic导入BaseModel
从…起导入模型
...
类TimeSheetRangeBaseModel:
用户id:int
开始:日期
总计:时间
类配置:
orm_模式=真
积垢
def get_timesheet_by_date_range_和_userdb:Session,user_id=int,date_start=datetime,date_end=datetime:
返回db.querymodels.TimeSheet.user\u id\
func.datemodels.TimeSheet.start.label'start'\
func.sec_to_timefunc.sumfunc.timediffmodels.TimeSheet.end,models.TimeSheet.start.label'total'\
.filtermodels.TimeSheet.user\u id==用户\u id\
.filterfunc.datemodels.TimeSheet.start>=日期\u开始\
.filterfunc.datemodels.TimeSheet.end='2020-09-20'
和
datetimesheet.end我认为这里的问题是,您使用的Pydantic模型需要时间对象类型,而它应该需要时间增量类型,因为您的sql语句似乎表明您正在计算总计列的时间差 因此,Pydantic schemas.py文件应为: 输入导入列表,可选 从日期时间导入日期、日期时间、时间增量 从pydantic导入BaseModel 从…起导入模型 ... 类TimeSheetRangeBaseModel: 用户id:int 开始:日期 总计:时间差 类配置: orm_模式=真
该死的,你是对的,奇怪的是,当我在mysql中得到我的sql输出时,它显示的是时间值而不是秒[{user_id:7,start:2020-09-20,total:64247},{user_id:7,start:2020-09-21,total:66071}]