How to enforce shallow copy for certain fields? #2457
-
This is a MWE where I am passing around an object of type import pydantic
class A(pydantic.BaseModel):
pass
class B(pydantic.BaseModel):
a: A
b1=B(a=A())
b2=B(a=b1.a)
assert id(b1.a)==id(b2.a) Is there a way around that? |
Beta Was this translation helpful? Give feedback.
Answered by
samuelcolvin
Mar 3, 2021
Replies: 1 comment 1 reply
-
see Config from pydantic import BaseModel
class A(BaseModel):
pass
class Config:
copy_on_model_validation = False
class B(BaseModel):
a: A
b1 = B(a=A())
b2 = B(a=b1.a)
assert id(b1.a) == id(b2.a) Note that:
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
eudoxos
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see Config
copy_on_model_validation
Note that:
copy_on_model_validation
on the inner model, not the outercopy_on_model_validation
is new in v1.8