I have a model instance which has an attribute called parent which is a string containing the parent's id.
When I serialise this instance, I want to wrap the parent id so that it appears as follows:
{
name = "John"
parent = {"id": "123-345"}
}
What is the correct way to do this in marshmallow?
This is what I am currently doing:
class IdSchema(Schema):
id = fields.String()
@pre_dump
def wrap(self, data, **_):
return {"id": data}
class UserSchema(Schema):
name = fields.String()
parent = fields.Nested(IdSchema)
const jsonString = JSON.stringify(userObject); console.log(jsonString);