Skip to content

Computed elements ignore order #283

@sasanjac

Description

@sasanjac

When serializing to xml, computed elements are dropped to the back of the element instead of where they are defined. Is there are workaround to that?

import pydantic_xml

class Base(pydantic_xml.BaseXmlModel, tag="Base"):
    @pydantic_xml.computed_element(tag="a")
    def a(self) -> str:
        return "asd"

    b: str = pydantic_xml.element(tag="b")
data = Base(b="fgh")

print(data.to_xml(encoding="UTF-8", exclude_none=True).decode())

results in

<Base><b>fgh</b><a>asd</a></Base>

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions