【发布时间】:2022-07-24 20:21:09
【问题描述】:
我正在开发一个网站,其中前端在 React 中完成,后端在 Python 中使用 FastAPI。我制作了一个表单,它需要一些数据并使用 axios 将其发送到后端。看起来是这样的
{
name='Jonathan',
aliases=["Johnny"],
birthdate='2-15-1980',
gender='male',
height=178
weight=90
nationalities=["American", "French"],
occupations=["Programmer", "Comedian"],
status='single',
images=[
{'attachment': FileList,
'location': 'Berlin',
'date': '10-14-2019'
}
]
}
但是,当我提交它时。 FastApi 似乎从表单中删除了图像。
name='Jonathan',
aliases=["Johnny"],
birthdate='2-15-1980',
gender='male',
height=178
weight=90
nationalities=["American", "French"],
occupations=["Programmer", "Comedian"],
status='single',
images=[
{'attachment': {'0': {}}, 'location': 'Berlin', 'date': '10-14-2019'}
]
这是当前路线的样子
@router.post("/register/user")
def register_user(user_data: UserCreate):
print(user_data)
我不完全确定发生了什么。我猜这与数据的发送方式及其加密方式有关。我在这里陷入了死胡同。提前致谢。
编辑:这就是 UserCreate Schema 的样子
class CharacterCreate(BaseModel):
name: str
aliases: list
birthdate: Optional[str]
gender: str
height: Optional[float]
weight: Optional[float]
nationalities: Optional[set[str]]
occupations: Optional[set[str]]
status: str
images: Optional[list]
【问题讨论】:
-
当您在前端代码中引用
FileList时,您是否检查了浏览器的开发工具(在网络下)实际提交给FastAPI 的内容?我猜你看到的就是你实际提交的,FileList并不能像你期望的那样序列化。 -
@Chris 用模型编辑了问题
-
@MatsLindh 澄清一下,
FileList似乎是一个内置的 JS 对象,而不是自定义对象。在请求负载中它只显示为images=[object Object] -
@Chris 是的。这些图像还应该包含一些关于它们的信息
-
@Chris 我看到了那个帖子,但它看起来像使用
Form(...)我需要单独接受每个字段,这会使函数有很多参数。有什么方法可以接受文件作为一个参数,而将表单的其余部分作为另一个参数?
标签: javascript html forms fastapi