Python 3.x 在序列化之前更改信息
我有一个数据库模型,如:-Python 3.x 在序列化之前更改信息,python-3.x,flask-sqlalchemy,marshmallow,flask-marshmallow,Python 3.x,Flask Sqlalchemy,Marshmallow,Flask Marshmallow,我有一个数据库模型,如:- class Script(db.Model): path = db.Column(db.String(50)) 和序列化程序,如:- class ScriptSchema(ma.Schema): class Meta: fields = ( 'path' ) 我的问题是,当我在查询后转储数据时:- all_scripts_orm = Script.query.all() all_scripts = ScriptSc
class Script(db.Model):
path = db.Column(db.String(50))
和序列化程序,如:-
class ScriptSchema(ma.Schema):
class Meta:
fields = (
'path'
)
我的问题是,当我在查询后转储数据时:-
all_scripts_orm = Script.query.all()
all_scripts = ScriptSchema(many=True).dump(all_scripts_orm)
我以
[
{"path": "Sample_folder/Sample_Script_1.txt"},
{"path": "Sample_folder/Sample_script_2.txt"}
]
但是我希望能够只提取脚本的名称并序列化它
[
{"path": "Sample_script_1.txt"},
...
]
由于我不想在脚本模型中为名称创建另一列,如何解决此问题?使用
函数
字段、文档示例和文档。例如:
from os import path as op
class ScriptSchema(ma.Schema):
class Meta:
fields = (
'path'
)
path = fields.Function(lambda obj: op.basename(obj.path))
非常感谢你,约翰·库宁汉姆。