pydantic-xml icon indicating copy to clipboard operation
pydantic-xml copied to clipboard

way to create mixed xml\non-xml models

Open hellozyemlya opened this issue 5 months ago • 2 comments

First of all, many thanks for good library. Question is - is there a way to create mixed model like

class MyModel(BaseModel):
    a: int
    b: str


class XmlModel(BaseXmlModel):
    b: MyModel = Field()
    a: str = attr()

where XmlModel.b is ignored when deserializing\serializing to xml?

Clarification - to be used with model_dump to dict, but ignored for xml.

hellozyemlya avatar Jul 25 '25 21:07 hellozyemlya

@hellozyemlya Hi,

Thanks for your feedback!

You should use PrivateAttr instead of Field:

class MyModel(BaseModel):
    a: int
    b: str


class XmlModel(BaseXmlModel):
    _b: MyModel = PrivateAttr()
    a: str = attr()

dapper91 avatar Jul 26 '25 07:07 dapper91

@dapper91 sorry, my question was clarified - the field must be still accounted in model_dump\model_validate.

hellozyemlya avatar Jul 27 '25 07:07 hellozyemlya