I'm facing some trouble using marshmallow to deserialize an object that has an attribute that is a list of object instances of itself.
I'm building a "query editor" and one of the objects I need has a list of "QueryFilters" (which will later be used to build my "WHERE" clause on the query). In the QueryFilter class I added a property that I called "nested_filters" that is a list of QueryFilter objects.
class QueryFilterSchema(Schema):
field_name = fields.Str()
table = fields.Str()
value = fields.Str()
value_source = EnumField(ValueSource)
comparison = EnumField(Comparison)
operator = EnumField(BooleanOperator)
nested_filters = fields.List(fields.Nested(__qualname__))
nested_filter_operator = EnumField(BooleanOperator)
def create_instance(self, data, **kwargs):
return QueryFilter(**data)
class ExtractorQuerySchema(Schema):
query_language = EnumField(QueryLanguage)
query_fields = fields.List(fields.Nested(QueryFieldSchema))
join_sources = fields.List(fields.Nested(QueryJoinSourceSchema))
filters = fields.List(fields.Nested(QueryFilterSchema))
@post_load
def create_instance(self, data, **kwargs):
return ExtractorQuery(**data)
The only problem I'm facing is, when I deserialize an object that has nested_filters, Python deserializes the whole "filters" property as a dictionary mapping.
Of course I can implement something to convert a dict to my class attribute by attribute, but is there a more automated way to accomplish this?
Ok, the problem was way more simpler than I thought:
The code was missing the @post_load decorator above the create_instance method at QueryFilterSchema class