With a Pydantic class as follows, I want to transform the foo
field by applying a replace
operation:
from typing import List
from pydantic import BaseModel
class MyModel(BaseModel):
foo: List[str]
my_object = MyModel(foo="hello-there")
my_object.foo = [s.replace("-", "_") for s in my_object.foo]
How can I do the replace
operation right within the class, when the object is created? Without Pydantic I would simply do that within __init(self, foo)
but since Pydantic creates its own __init__
implementation I'm not sure how to proceed exactly.