Skip to content

Marshmallow hooks on schema inheritance #2031

@codectl

Description

@codectl

How can we chain Marshmallow hooks when you use schema inheritance?

Suppose the following "dumb" example:

from marshmallow import Schema, fields, post_load


class PersonSchema(Schema):
    name = fields.Str()

    @post_load
    def post_load(self, data, **_):
        data["name"] = data["name"].capitalize()
        return data


class EmployeeSchema(PersonSchema):
    title = fields.Str()

    @post_load
    def post_load(self, data, **_):
        data["title"] = data["title"].upper()
        return data

EmployeeSchema().load({"name": "john", "title": "ceo"})
# {"name": "john", "title": "CEO"}

In this example, only the subclassed hook was triggered. How do you make the parent hook trigger as well on deserialization, in this case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions