Problems converting Pydantic model v1 to v2 #8298
Unanswered
HelmutKarsten
asked this question in
Question
Replies: 1 comment
-
@HelmutKarsten Hey there, I encountered the same problem, and here's how I solved it with class OrmModelGetter:
def __init__(self, value: models.MyOrmModel):
self._value = value
def __getattr__(self, item: str):
# TODO: your own get attribute logic here
return getattr(self._value , item)
class MyObj(BaseModel):
id: str
message: str
model_config = ConfigDict(from_attributes=True)
@pydantic.model_validator(mode="before")
def transform_model(cls, value: typing.Any):
if isinstance(value, models.MyOrmModel):
return OrmModelGetter(value)
return value |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi everyone,
we use Pydantic together with SqlAlchemy. While trying to upgrade Pydantic v1 to v2 I came across a tricky Problem, where I'm not sure how to handle the situation.
We have a class (V1) called
OrmMixin
The
MutableGetterDict
allows us later to use theget
Function on an SqlAlchemy model. Which comes in handy when doingvalidations previously in the
root_validator
andvalidator
functions.Since v2 removed
orm_mode
andgetter_dict
the type that mymodel_validator
gets is the type of my SqlAlchemy model.All places we use a
get
in our validators fail now.Do you have a good example on how to fix this issue without rewriting all validators using the properties of the
getter_dict
?Beta Was this translation helpful? Give feedback.
All reactions