marshmallow-code / marshmallow-code/marshmallow

Serialize multiple attributes using Pluck

Open
#1,818 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Can Pluck be used to deserialize multiple source attributes to a single field and then serialize back to multiple fields? I read in #1315 that the intent of Pluck is to go from flat -> nested -> flat, but I couldn't figure out how to get from the input:

`{'baz': 'blue', 'qux': 'orange'}`

by deserializing to

`{'bar': {'baz': 'blue', 'qux': 'orange'}}`

and then serializing back to the above.

I could get _either_`baz` or `qux` but not both.

I attempted something like

```
class Bar(Schema):
baz = fields.String()
qux = fields.String()

class Foo(Schema):
bar_1 = fields.Pluck(Bar, 'baz', data_key='baz', attribute='bar')
bar_2 = fields.Pluck(Bar, 'qux', data_key='qux', attribute='bar')
```

But this throws the error:

`ValueError: The attribute argument for one or more fields collides with another field's name or attribute argument. Check the following field names and attribute arguments: ['bar']`

Is there something I'm missing, or is this use case not supported by Pluck?

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 by reading fields.Pluck and the behavior discussed in issue #1315, then reproduce the Bar and Foo schemas from this report. Verify whether multiple Pluck fields can share the bar attribute while deserializing and serializing both baz and qux; done means the reported round trip works or the limitation is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.