python config_error ai_generated true

pydantic.errors.ConfigError: 模型不可变

pydantic.errors.ConfigError: model is immutable

ID: python/pydantic-model-config-immutable

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

版本兼容性

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

根因分析

尝试修改配置了 frozen=True 的模型的字段,引发 ConfigError。

English

Attempting to modify a field of a model that has frozen=True in config, raising ConfigError.

generic

解决方案

  1. 95% 成功率
    Remove frozen=True from model config
  2. 90% 成功率
    Use model_copy(update={'field': new_value}) to create a new instance

无效尝试

常见但无效的做法:

  1. 90% 失败

    Frozen models disallow attribute setting.

  2. 40% 失败

    This works but doesn't modify original; may be acceptable.