You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That is, the metadata consists of a dynamic number of elements with dynamic tags and no attributes, each of which contains just text.
Ideally, I would want this to map to the Python model
class Article:
title: str
metadata: Dict[str, str]
is there a way to achieve that with pydantic-xml? The closest I got so far was by making metadata a raw field, but then working from the Python side gets a little annoying: how do I construct an instance of Article when metadata is ET.Element? I could create a new constructor class method but then I'd have to remember that for this specific model only, I shouldn't use the constructor.
The other approach I expected to work was setting metadata=Field(exclude=True) and implementing a @computed_element for serialization, and a @field_validator for deserialization. Unfortunately the @field_validator approach doesn't work:
Imagine I have the following xml:
That is, the metadata consists of a dynamic number of elements with dynamic tags and no attributes, each of which contains just text.
Ideally, I would want this to map to the Python model
is there a way to achieve that with pydantic-xml? The closest I got so far was by making
metadata
a raw field, but then working from the Python side gets a little annoying: how do I construct an instance of Article whenmetadata
isET.Element
? I could create a new constructor class method but then I'd have to remember that for this specific model only, I shouldn't use the constructor.The other approach I expected to work was setting
metadata=Field(exclude=True)
and implementing a @computed_element for serialization, and a @field_validator for deserialization. Unfortunately the @field_validator approach doesn't work:prints
i.e. the validator receives an empty dict, not anything that could reconstruct the inner fields.
Is there a currently supported approach that I'm missing?
Thanks!
The text was updated successfully, but these errors were encountered: