Define fields that are not arguments to the constructor #5929
-
I'd like to define a field that is not available in the model arguments. So to define an instance attribute with either a default or a default factory, and no way to change it when constructing the model (assigning is fine). To essentially achieve this: class Foo:
def __init__(self, a: int, b: str = ""):
self.a = a
self.b = b
self.c = generate_c() # or without the call as just a default value Does Pydantic support this? I didn't find relevant arguments to class Foo(BaseModel):
a: int
b: str = ""
c: int
class Bar(Foo):
c: int = Field(default_factory=list, no_init=True) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
We do not currently support this, but dataclasses do (via |
Beta Was this translation helpful? Give feedback.
-
Also, |
Beta Was this translation helpful? Give feedback.
-
I would also like |
Beta Was this translation helpful? Give feedback.
-
Pydantic uses |
Beta Was this translation helpful? Give feedback.
The support has been added in 2.6 it seems!