serializer
serializer copied to clipboard
Dynamic entry_name based on type of child property
Hello Johnannes,
first of all I wanted to thank you personally for the serializer. We are using it successfully in 2 projects and really love it.
Currently I am facing a problem and hope to get help here in the group.
I have a user object in which I have stored favourite dishes of the users as a list.
An example:
<user firstname="John" lastname="Doe">
<favourites>
<pizzas>
<pizza name="Pizza Hawaii" />
</pizzas>
<pastas>
<pasta name="Lasagne" />
</pastas>
</favourites>
</user>
I need the favorite foods in a flat list now though, so in this case:
<favourites>
<pizza name="Pizza Hawaii" />
<pasta name="Lasagne" />
</favourites>
For this I implemented a getItems() method in my Favourites object instead of getPizzas()and getPastas(), which returns me dishes of type "Pizza" or "Pasta".
Now how can I implement that I can set the entry_name in the list depending on the child object.
My Favourites specification looked like this before:
pizzas:
expose: true
accessor:
getter: getPizzas
xml_list:
entry_name: pizza
pastas:
expose: true
accessor:
getter: getPastas
xml_list:
entry_name: pasta
The new specification now looks like this:
items:
expose: true
accessor:
getter: getItems
xml_list:
entry_name: <pizza or pasta based on the type>
Pizza and Pasta object now has a "type" property storing "pizza" and "pasta", I did already exclude it in the child types because the structure here is given and it cannot be added as a new attribute type.
How can this be achieved?
Thanks for your help!
Best regards, Sven
Hi @svenhaack !
I think inline
option under xml_list
will help you to achieve your goal.
See: yml_reference.rst
Best, scyzoryck.