为响应模式 fastapi 构建 Pydantic 模式

发布于 2025-01-19 20:44:11 字数 1417 浏览 1 评论 0原文

我有这样的数据响应

{
  "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2025-01-26 20:44:11

首先将列表(字典)响应和卡结构分开:

class Card(BaseModel):
    card_name: str
    price_updated_date: datetime.datetime,
    card_img_id: str
    set_name: str
    set_tag: str
    rarity: str
    price_normal: float  # or maybe better, decimal.Decimal  - depends on your use case.
    price_foil: float  # same

然后,您可以在search Decorator中直接使用它,然后说您返回卡片词典:

@router.get("/{item_name}", response_model=Dict[str, Card])
async def search(item_name):
    pass

但是,您应该能够使用<代码> __根__ 定义a

class CardListResponse(BaseModel):
    __root__: Dict[str, Card]


@router.get("/{item_name}", response_model=CardListResponse)
async def search(item_name):
    pass

Start by separating the list (dictionary) response and the card structure:

class Card(BaseModel):
    card_name: str
    price_updated_date: datetime.datetime,
    card_img_id: str
    set_name: str
    set_tag: str
    rarity: str
    price_normal: float  # or maybe better, decimal.Decimal  - depends on your use case.
    price_foil: float  # same

You can then use this in your search decorator directly and say that you return a dictionary of cards:

@router.get("/{item_name}", response_model=Dict[str, Card])
async def search(item_name):
    pass

However, you should be able to use __root__ to define a root level dictionary response:

class CardListResponse(BaseModel):
    __root__: Dict[str, Card]


@router.get("/{item_name}", response_model=CardListResponse)
async def search(item_name):
    pass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文