According to the docs:
allow_mutation
whether or not models are faux-immutable, i.e. whether setattr is allowed (default: True)
Well I have a class :
class MyModel(BaseModel):
field1:int
class Config:
allow_mutation = True
If I try to add a field dynamically :
model1 = MyModel(field1=1)
model1.field2 = 2
And I get this error :
File "pydantic/main.py", line 347, in pydantic.main.BaseModel.__setattr__
ValueError: "MyModel" object has no field "field2"
Obviously, using setattr
method will lead to the same error.
setattr(model1, 'field2', 2)
Output:
File "pydantic/main.py", line 347, in pydantic.main.BaseModel.__setattr__
ValueError: "MyModel" object has no field "field2"
What did I miss here ?