python - 使用数据库中不存在的字段嵌套在 Python 和 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”和“mother”与“user_id”和“name”在同一个表中,但“parents”只是JSON的一个属性:
{
"data": [
{
"userID": 1,
"nameUser": "Guido",
"parents": {
"father": "John",
"mother": "Marie"
}
}
...
]
}
我用“嵌套”类尝试过,但它不起作用。那么当模型中不存在“parents”属性时如何进行嵌套呢?
解决方案
我想出了答案,基本上是这样的:
parents = fields.Function(lambda x : {'mother': x.mother, 'father': x.father})
推荐阅读
- node.js - 放大配置 - 错误:uncaughtException: spawn cmd ENOENT
- css - 在 CSS 动画中混合颜色
- node.js - 使用 Node.js 通过 json 中的变量插入值
- uml - 更好的 UML 类图结构来表示个人的婚姻
- sql - Oracle:事务表中的新用户计数
- autohotkey - 如何使用 AHK 发送正斜杠
- c++ - 使用复制构造函数 C++ 时出现分段错误
- python - 带有 URL 路径变量的 Python http 删除请求
- ionic-framework - 检查是否通过其 id 打开了任何模态,然后关闭该模态 IONIC
- python - QWebEnginePage 与 javascript 交互不起作用?