List(Nested()) doesn't use `many` in the nested schema

Author: ThiefMasterCreated Apr 30, 2021Updated Nov 10, 2025
python
from marshmallow import Schema, fields, post_dump


class ItemSchema(Schema):
    id = fields.Integer()
    name = fields.String()

    @post_dump(pass_many=True)
    def query_extra_data(self, data, many, **kwargs):
        print(f'post_dump called: {many=} {data=}')


class TestSchema(Schema):
    items = fields.List(fields.Nested(ItemSchema))


data = {'items': [
    {'id': 1, 'name': 'foo'},
    {'id': 2, 'name': 'bar'},
    {'id': 3, 'name': 'snafu'},
]}

TestSchema().dump(data)

This gives me the following output:

post_dump called: many=False data={'name': 'foo', 'id': 1}
post_dump called: many=False data={'name': 'bar', 'id': 2}
post_dump called: many=False data={'name': 'snafu', 'id': 3}

However, I expected this:

post_dump called: many=True data=[{'name': 'foo', 'id': 1}, {'name': 'bar', 'id': 2}, {'name': 'snafu', 'id': 3}]

This is a problem when the post_dump hook queries stuff from a database as I would end up with n individual queries instead of a single one where I can use IN to efficiently get data for all the objects in the list.

Are there any decent workarounds for this? One thing that came to my mind is using this instead of List(Nested), but it feels much uglier...

python
items = fields.Function(lambda data: ItemSchema(many=True).dump(data['items']))

Source: marshmallow-code/marshmallow