question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Marshmallow doesn't merge nested data

See original GitHub issue
from marshmallow.schema import Schema, fields
class MyPayload(Schema):
    class ActionAdd(Schema):
        # Parameters for actionAdd
        item_name = fields.Str()

    class ActionRemove(Schema):
        # Parameters for actionRemove
        ref_number = fields.Int()

    add_items = fields.Nested(ActionAdd, many=True, dump_to="items")
    remove_items = fields.Nested(ActionRemove, many=True, dump_to="items")


data = {
    "add_items": [
        {
            "item_name": "notebook"
        }
    ],
    "remove_items": [
        {
            "ref_number": 10
        }
    ]
}

request_data, errors = MyPayload().dump(data)

>>> request_data
Out[3]: {'items': [{'item_name': 'notebook'}]}

In an example shown above I am trying to dump a dictionary that will construct a payload to my API. the set of attributes of “ActionAdd” is differ from “ActionRemove”. They both need to be dumped to the same “items” dictionary. From the example you can see that the request_data is missing “ref_number”. Once “item_name” is removed from “data” then “ref_number” will be visible in request_data. Whats wrong with my implementation ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Jun 7, 2018

I’m not sure that we should implicitly merge when duplicate keys are used for dump_to. I think we should error instead of silently overwriting data.

Have you tried implementing this behavior with a post_dump method instead of dump_to?

from marshmallow import Schema, fields, post_dump

class MyPayload(Schema):
    class ActionAdd(Schema):
        item_name = fields.Str()
    
    class ActionRemove(Schema):
        ref_number = fields.Int()
    
    add_items = fields.Nested(ActionAdd, many=True)
    remove_items = fields.Nested(ActionRemove, many=True)
    
    @post_dump
    def merge_items(self, data):
        data['items'] = data['add_items'] + data['remove_items']
        del data['add_items']
        del data['remove_items']
        return data

If the use case ever needs special ordering or conditional logic, this pattern would have to be used anyway.

0reactions
lafrechcommented, Oct 12, 2018

In marshmallow v3, an error is raised in case of attribute or data_key collision (excluding dump_only/load_only fields): https://github.com/marshmallow-code/marshmallow/pull/992.

A custom field seems relevant to your need. Feel free to reopen if still stuck with this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to merge a nested object with parent in marshmallow (+ ...
You would have to serialize each property of the nested object separately. The easiest way, if you wanted to go this route, would...
Read more >
Nesting Schemas — marshmallow 3.19.0 documentation
Schemas can be nested to represent relationships between objects (e.g. foreign key relationships). For example, a Blog may have an author represented by...
Read more >
marshmallow -- simplified object serialization | We all are data.
Use a Nested field to represent the relationship, passing in a nested schema. from marshmallow import Schema, fields class UserSchema(Schema): ...
Read more >
Object validation and conversion with Marshmallow in Python
Marshmallow is a Python library that converts complex data types to ... + os.path.join(BASE_DIR, 'db.sqlite3') db = SQLAlchemy(app) # Add ...
Read more >
Manage manifest files - Android Developers
Learn about manifest build variables, manifest merge, and the manifest ... the final merged manifest does not include content from the manifests of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found