Skip to content

[Question] serialize/deserialize a computed element #259

Description

@lionpeloux

Hi there,

First of all thank you for this great lib !
I've been trying for hours to do what looks like a very simple thing, but could not succeed :

a and b are passed at init time Test(a="A", b="B").
The computed element is correctly serialize to xml to A#B
But how to do deserialization, that is assigned back A and B from A#B?
I've tried many options, playing wing with custom validators and sertializers but could not make it, including the ones from pydantic (model_post_init, @model_validator, ...)

Any hint would be awsome !

PS : think of it as last_name + first_name = full_name

class Test(BaseXmlModel):  
  a: str | None = Field(exclude=False, default=None)
  b: str | None = Field(exclude=False, default=None)
  
  @computed_element(tag='a_and_b')  # type: ignore
  def a_and_b(self) -> str:
    return f"{self.a or ''}#{self.b or ''}"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions