我在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"one_answers";mother"与"user_id"在同一个表中和"姓名",但"父母";是JSON的一个属性:
{
"data": [
{
"userID": 1,
"nameUser": "Guido",
"parents": {
"father": "John",
"mother": "Marie"
}
}
...
]
}
我试着用"Nested"类,但它不起作用。那么如何在嵌套"父"属性在模型中不存在?
我找到了答案,基本上是这样的:
parents = fields.Function(lambda x : {'mother': x.mother, 'father': x.father})