Pydantic copies a model when passing it to the constructor of another model. This fails:
from pydantic import BaseModel
class Child(BaseModel):
pass
class Parent(BaseModel):
child: Child
child = Child()
parent = Parent(child=child)
assert parent.child is child
# Fails
It seems child
is copied when passing it to the parent's constructor and therefore the identities of child
and parent.child
are not the same. I would like to have them to be the same as I need to modify child
's attributes later and the changes should be seen in parent.child
.
How do I make Pydantic not copy the child?