python install_error ai_generated true

pydantic.errors.PydanticUserError: `from_orm`已弃用;请使用`model_validate`并设置`from_attributes=True`

pydantic.errors.PydanticUserError: `from_orm` is deprecated; use `model_validate` with `from_attributes=True`

ID: python/fastapi-pydantic-v2-orm-mode

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-03-30首次发现

版本兼容性

版本状态引入弃用备注
2.x active — — —

根因分析

在安装了Pydantic v2的情况下使用了Pydantic v1 API,通常是升级FastAPI后。

English

Pydantic v1 API used with Pydantic v2 installed, typically after upgrading FastAPI.

generic

解决方案

  1. 95% 成功率
    class UserOut(BaseModel):\n    model_config = ConfigDict(from_attributes=True)\n    id: int
  2. 90% 成功率
    UserOut.model_validate(user_obj)

无效尝试

常见但无效的做法:

  1. 70% 失败

    FastAPI >=0.100 requires pydantic v2; other errors appear.

  2. 85% 失败

    orm_mode is a v1 setting; ignored in v2.