簡體   English   中英

如何更改獲取列表的輸出,或在 FastAPI 中使用備用序列化程序?

[英]How to alter the ouput of a get list, or use an alternate serializer in FastAPI?

我正在嘗試根據 get 參數更改 FastAPI 上列表視圖的內容。 由於格式由 pydantic model 定義,我如何自定義它(或在視圖中使用替代 model)?

這是我的觀點:

from fastapi_pagination import Page, Params, paginate
from pydantic import BaseModel
from sqlalchemy.orm import Session


class EventSerializer(BaseModel):
    id: str
    # ...

class EventAttendeeSerializer(BaseModel):
    id: str
    event: str  # contains the event UUID
    # ...

    class Config:
        orm_mode = True


@api.get("/", response_model=Page[EventAttendeeSerializer])
async def get_list(db: Session, pagination: Params = Depends(), extend: str = None):
    objects = db.query(myDbModel).all()
    if "event" in extend.split(","):
        # return EventSerializer for each object instead of id
    
    return paginate(objects, pagination)

在運行時,它會像這樣工作:

GET /v1/event-attendees/
{
    "items": [
        {
            "id": <event_attendee_id>,
            "event": <event_id>,
        }
    ],
    "total": 1,
    "page": 1,
    "size": 50,
}
GET /v1/event-attendees/?extend=event
{
    "items": [
        {
            "id": <event_attendee_id>,
            "event": {
                "id": <event_id>,
                # ...
            }
        }
    ],
    "total": 1,
    "page": 1,
    "size": 50,
}

我在 pydantic 和 FastAPI 文檔和源代碼中搜索了某種掛鈎,但沒有找到任何相關內容。 有人可以幫忙嗎?

您可以使用Union (兩種類型)對response_model進行decalre,並在滿足條件時返回您希望的 model。 由於您要返回對象/模型列表,因此可以將response_model聲明為Union List

工作示例

from fastapi import FastAPI
from pydantic import BaseModel
from typing import List, Union


class Simple(BaseModel):
    id: int


class Specific(Simple):
    description: str


RESULT = {
    'id': 1,
    'description': 'test'
}


app = FastAPI()

    
@app.get('/results', response_model=List[Union[Specific, Simple]])
def get_results(specific: bool = False):
    if specific:
        results = [Specific(**RESULT)] * 2
    else:
        results = [Simple(**RESULT)] * 2

    return results

使用 FastAPI 0.89.0+ ,您可以選擇在 function 返回類型注釋中聲明返回類型 / response_model ,例如:

@app.get('/results')
def get_results(specific: bool = False) -> List[Union[Specific, Simple]]:
    if specific:
        # ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM