2

I have these schemas in Marshmallow:

class Parents(Schema):
     father = fields.String(data_key="father")
     mother = fields.String(data_key="mother")

class UserSchema(Schema):
     user_id = fields.Integer(data_key="userID")
     name = fields.String(data_key="nameUser")
     parents = Nested(Parents, many=True)

"father" and "mother" are in the same table as "user_id" and "name", but "parents" is just an attribute for JSON:

{
    "data": [
        {
            "userID": 1,
            "nameUser": "Guido",
            "parents": {
                "father": "John",
                "mother": "Marie"
            }
        }
        ...
    ]
}

I tried it with the "Nested" class but it doesn't work. So how to do this nesting when the "parents" attribute doesn't exist in the models?

1 Answers1

2

I figured out the answer, basically it's this:

parents = fields.Function(lambda x : {'mother': x.mother, 'father': x.father})