I use pydantic in a FastAPI project to serialize SQLAlchemy entity objects, and I need to access the original entity object in @computed_field
methods and validators in order to access data from relationships (which are not supposed to be themselves serialized).
This is what I tried so far:
def __init__(__pydantic_self__, **kwargs) -> None:
super().__init__(**kwargs)
print(kwargs) # not called at all
@model_validator(mode="before")
@classmethod
def validate_model(cls, values):
# 'values' contains the desired entity object, but there is no access to the model instance,
# so I can't save it for future use
@computed_field
@property
def extra_prop(self) -> bool:
# no access to the entity object
def model_post_init(self, ctx):
print(ctx) # always None
Is there any supported way to do this, or a workaround known to work?