Skip to content

How to enforce shallow copy for certain fields? #2457

Answered by samuelcolvin
eudoxos asked this question in Question
Discussion options

You must be logged in to vote

see Config copy_on_model_validation

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:

  • you need to declare copy_on_model_validation on the inner model, not the outer
  • copy_on_model_validation is new in v1.8

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@eudoxos
Comment options

Answer selected by eudoxos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants