我需要在API返回值时为每个字段按字母顺序排序。似乎marshmallow的pre_dump方法是在序列化之前预处理数据的方法,但我还没有搞清楚。我已经多次阅读了文档并进行了谷歌搜索,但没有找到答案。
class UserSettingsSchema(UserSchema):
class Meta:
fields = (
"id",
"injuries",
"channel_levels",
"movement_levels",
"equipments",
"goals",
"genres"
)
@pre_dump
def pre_dump_hook(): # what do I pass in here?
# alphabetize field values, eg, all the genres will be sorted
equipments = fields.Nested(EquipmentSchema, many=True)
goals = fields.Nested(GoalSchemaBrief, many=True)
genres = fields.Nested(GenreSchemaBrief, many=True)