为响应模式 fastapi 构建 Pydantic 模式
我有这样的数据响应
{
"k9KNg_id": {
"card_name": "item1",
"price_updated_date": "2022-04-07T19:30:25.78Z",
"card_img_id": "https://testingimg",
"set_name": "some testing set",
"set_tag": "mm3",
"rarity": "rare",
"price_normal": 32.99,
"price_foil": 54.99
},
"EWrZ5_id": {
"item_name": "item2",
"price_updated_date": "2022-04-07T08:05:52.385Z",
"item_name_img_id": "https://testingimg",
"set_name": "testing set",
"set_tag": "exp",
"rarity": "super rare",
"price_normal": null,
"price_foil": 379.99
},
,所以现在我尝试使用带有基本模型架构的 fastapi 编写此 api 返回。我应该如何编写架构?因为我有我的商品 ID,然后是我的商品详细信息。
item_dict.update({
"item_name": abc["item"]["name"],
"price_updated_date": ed["prices"]["lastUpdatedAtUtc"],
"item_img_id": item_img_url,
"set_name": ed["name"],
"set_tag": ed["abbreviation"],
"rarity": ed["rarity"],
"price_normal": normal_price,
"price_foil": foil_price
})
data_return[ed["card_id"]] = ckd_dict
return data_return
在我的 api 调用中,我只是发送一个非常简单的 fastapi 调用,
@router.get("/{item_name}")
async def search(item_name):
item = get_item(item_url, item_url_type, item_name)
return item
我想为我的 fastapi 创建一个 response_model 以返回数据,但我不确定如何使用这种数据结构构建我的模式
i have a response of data like this
{
"k9KNg_id": {
"card_name": "item1",
"price_updated_date": "2022-04-07T19:30:25.78Z",
"card_img_id": "https://testingimg",
"set_name": "some testing set",
"set_tag": "mm3",
"rarity": "rare",
"price_normal": 32.99,
"price_foil": 54.99
},
"EWrZ5_id": {
"item_name": "item2",
"price_updated_date": "2022-04-07T08:05:52.385Z",
"item_name_img_id": "https://testingimg",
"set_name": "testing set",
"set_tag": "exp",
"rarity": "super rare",
"price_normal": null,
"price_foil": 379.99
},
so now i am trying to write this api return with fastapi with a basemodel schema. how should i write the schema? since i have my item id and then my item product details.
item_dict.update({
"item_name": abc["item"]["name"],
"price_updated_date": ed["prices"]["lastUpdatedAtUtc"],
"item_img_id": item_img_url,
"set_name": ed["name"],
"set_tag": ed["abbreviation"],
"rarity": ed["rarity"],
"price_normal": normal_price,
"price_foil": foil_price
})
data_return[ed["card_id"]] = ckd_dict
return data_return
and on my api call i just send a very simple fastapi call
@router.get("/{item_name}")
async def search(item_name):
item = get_item(item_url, item_url_type, item_name)
return item
i wanted to create a response_model for my fastapi to return the data but i am not sure how to construct my schema with this kind of data structure
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先将列表(字典)响应和卡结构分开:
然后,您可以在
search
Decorator中直接使用它,然后说您返回卡片词典:但是,您应该能够使用<代码> __根__ 定义a :
Start by separating the list (dictionary) response and the card structure:
You can then use this in your
search
decorator directly and say that you return a dictionary of cards:However, you should be able to use
__root__
to define a root level dictionary response: