Pydantic ObjectId из mongodb во вложенной модели

Коллеги, столкнулся со следующей проблемой, есть эндпоинт:

from schemas import SalesKpiResponse as SalesKpiResponseSch
@ns.get("/{sales_kpi_id}", response_model=SalesKpiResponseSch)
async def _get_sale_kpi_by_id(sales_kpi_id: str):
    sales_kpi = await get_sales_kpi_by_id(sales_kpi_id)
    if sales_kpi is None:
        raise HTTPException(status_code=404, detail="Object Not found")
    return sales_kpi

Модель сущности и модель ответа выглядит так:

class SalesKpi(BaseModel):
    name: str
    target_assortment: list[str]
    is_group_kpi: bool
    target_department: list[str] = []

    class Config:
        allow_population_by_field_name = True


class SalesKpiResponse(SalesKpi):
    id: ObjectIdField = Field(default_factory=ObjectIdField, alias="_id")
    foo: str = None

    class Config:
        allow_population_by_field_name = True
        arbitrary_types_allowed = True
        orm_mode = True
        json_encoders = {ObjectId: str}

Проблема в том, что мне нужно в ответе получать id, а приходит _id

Ответ выглядит так :

{
  "name": "string",
  "target_assortment": [
    "string"
  ],
  "is_group_kpi": true,
  "target_department": [],
  "_id": "63f130901e5c94bd93dee133",
}

 До этого, когда не было вложенности в моделях, все отображалось корректно. Что нужно исправить?


Ответы (0 шт):