By default, marshmallow uses marshmallow.utils.get_value to retrieve attributes from objects during serialization. If you need to change this behavior—for example, to ensure that attribute access always uses a specific method like dict.get when serializing dictionaries—you can override the get_attribute method on your Schema class.
When overriding get_attribute(self, obj, key, default), you are responsible for implementing the logic to extract the value for the given key from the object obj, returning the default value if the key is not found.
class UserDictSchema(Schema):
name = fields.Str()
email = fields.Email()
# If we know we're only serializing dictionaries, we can
# use dict.get for all input objects
def get_attribute(self, obj, key, default):
return obj.get(key, default)