python
config_error
ai_generated
true
PydanticUserError: `orm_mode` 已移除,请改用 `from_attributes=True`。
PydanticUserError: `orm_mode` is removed. Use `from_attributes=True` instead.
ID: python/fastapi-pydantic-config-orm-mode
80%修复率
89%置信度
0证据数
2024-05-09首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
Pydantic 模型在 v2 下仍使用 v1 的 `orm_mode = True` 配置。
English
A Pydantic model still uses v1's `orm_mode = True` Config under v2.
解决方案
-
93% 成功率
Use v2 config: class UserOut(BaseModel): model_config = ConfigDict(from_attributes=True) -
85% 成功率
Validate manually with model_validate(obj, from_attributes=True).
无效尝试
常见但无效的做法:
-
90% 失败
orm_mode is rejected outright, so the model fails to import.
-
70% 失败
Then ORM objects can't be validated from attributes, breaking response_model.