Is it possible to load from attributes? #185
Replies: 2 comments
-
|
Hi @fabianoengler! Thanks for starting the discussion — this sounds like a potential enhancement, but I want to make sure I understand the exact use case before implementing anything. When you say “load from attributes”, could you clarify what the input object looks like? For example, are you loading from:
Also, should attribute access replace dict access, or only be used as a fallback? I’ll open a tracking issue for this and link it here once the expected behavior is clearer. |
Beta Was this translation helpful? Give feedback.
-
|
@fabianoengler I’ve opened a tracking issue here: #228 — feel free to add more details or examples there. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Pydantic has a
from_attributes=True[1] option I can pass to load models (or set that as a config) that causes the loaded values to be loaded with attribute access instead of field access (object.fieldinstead ofobject['field']).This is pretty awesome because it allows me to load from almost any object type, since most objects will have attributes that can access, in particular this allows me to load from ORM objects (like SQLAlchemy), other pydantic models and dataclasses.
I've went through dataclass-wizard documentation and didn't find anything like that, I'm wondering if that's possible with dataclass-wizard and if not, if there are any plans to implement this in the future.
[1] https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_validate
Beta Was this translation helpful? Give feedback.
All reactions