【问题标题】:Python / pydantic / FastAPI - describe this datastructure as a Schema Model?Python / pydantic / FastAPI - 将此数据结构描述为模式模型?
【发布时间】:2022-06-21 20:31:44
【问题描述】:

问题

我有一个嵌套列表的数据结构,我正在尝试使用 Pydantic 模型在 FastAPI 中构建一个response_model,但到目前为止证明是不可能的。一旦我应用 response_model= 指令,数据就会从 API 返回为空。如果我删除 response_model= 指令,对 API 的相同查询会导致数据返回得很好并且有内容。

数据如下所示:

[
    {
        'code': 'trabant',
        'description': 'East German Trabant',
        'listings': [
            {
                 id: 1000,
                 cat_no: "Trabi1",
                 descript: "Trabant Interior Mirror"
                 price: Decimal(16.95),
                 veh: 'trabant',
                 sec: 'interior'
             },
             {
                 id: 1001
                 cat_no: "Trabi2",
                 descript: "Trabant Interior Clock"
                 price: Decimal(56.95),
                 veh: 'trabant',
                 sec: 'interior'
             }
         ]
    },
    {
        'code': 'skoda',
        'description': 'Czech Skoda',
        'listings': [
            {
                  id: 2001,
                  cat_no: "Skoda5",
                  descript: "Front Grille",
                  price: Decimal(36.95),
                  veh: 'skoda',
                  sec: 'bodywork'
             },
             {
                  id: 2002
                  cat_no: "Skoda6",
                  descript: "Skoda Grille Badge - Front"
                  price: Decimal(16.95),
                  veh: 'skoda',
                  sec: 'bodywork'
             }
        ]
    }
]

归结为它的结构如下:

] # root list
    { #can be any vehicle in a list of 40+
        'code': #vehicle's db code
        'description': #vehicle's textual name>,
        'listings': [ #list of catalogue items for this vehicle
            {
                id: #db id,
                cat_no: #Customer SKU,
                descript: #Description of a part,
                price: #Decimal value for price, no evil floats here!,
                veh: #db code for vehicle,
                sec: #db code for section
            }
        ]
    }
]

我尝试使用这些 Pydantic 模型来描述它:

class ORMBaseModel(BaseModel):
    class Config:
        orm_mode = True

class CatListing(ORMBaseModel):
    id: int
    cat_no: str
    descript: str
    sec: str
    veh: str
    price: Decimal

class VehicleCatListings(ORMBaseModel):
    code: str
    description: str
    listings: List[ CatListing ]

class ListOfCatListings(ORMBaseModel):
    List[ VehicleCatListings ]

但是当我使用以下路线时:

@app.get("/api/cat_no/{ff_no}/listings", response_model=schema.ListOfCatListings)
def getListings(ff_no: str, db: Session = Depends(databases.getDb)):
    listings = crud.catalogue.getListings(db, ff_no) #db request that returns data output as shown above

    if listings is None:
        raise HTTPException(status_code=404, detail="FF No catalogue listings not found")
    
    return listings

我得到一个空白对象{} 作为回报,好像 pydantic 模型以某种方式忽略了数据。但是我发现很难调试。

注意:我不在一个销售稀有东欧汽车零件的地方工作,我只是以这些为例;)

【问题讨论】:

    标签: python fastapi pydantic


    【解决方案1】:

    好吧,在我用头撞了一堵砖墙之后,事实证明这是一个非常常见的问题,并且已经在 Stackoverflow 上多次提到过。这里的错误在于:

    class ListOfCatListings(ORMBaseModel):
        List[ VehicleCatListings ]
    

    应该是:

    class ListOfCatListings(ORMBaseModel):
        __root__: List[ VehicleCatListings ]
    

    这满足了我的需要。

    我希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2021-10-12
      • 2022-08-19
      • 2021-06-10
      • 1970-01-01
      • 2021-03-04
      相关资源
      最近更新 更多