marshmallow-code / marshmallow-code/marshmallow

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

Open
#1,801 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

```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']))
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the List(Nested(...)) serialization path and the @post_dump(pass_many=True) hook shown in the report. Reproduce the example with TestSchema().dump(data), then inspect how fields.List and fields.Nested propagate many; done means the nested hook receives one batched call without changing existing scalar behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.