I am trying the marshmallow ORM library from here. I have a general reporting schema with this form:
class ReportingQueryResultSchema(ma.Schema):
start_date = fields.Str(required=True)
end_date = fields.Str(required=True)
data = fields.Nested(
ReportingDataSchema,
required=True,
many=True
)
ReportingDataSchema is a generic field. It can be different types of schema. My first instinct here is inheritance. So I try something like this:
class ReportingDataSchema(ma.Schema):
pass
class FooSchema(ReportingDataSchema):
value = fields.Str()
class BarSchema(ReportingDataSchema):
thing = fields.Str()
marshmallow fails to serialize the nested fields, giving me the following:
{
"data": [
{}
],
"end_date": "2017-06-05",
"start_date": "2017-06-01"
}
But if I declare ReportingQueryResultSchema as:
class ReportingQueryResultSchema(ma.Schema):
start_date = fields.Str(required=True)
end_date = fields.Str(required=True)
data = fields.Nested(
FooSchema, # replaced here
required=True,
many=True
)
It is then able to detect the nested schema and properly populate the result.
{
"data": [
{"key": "val"}
],
"end_date": "2017-06-05",
"start_date": "2017-06-01"
}
What is the correct way to do generic nested schema in marshmallow?