Marshmallow doesn't merge nested data
See original GitHub issuefrom 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:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 ofdump_to
?If the use case ever needs special ordering or conditional logic, this pattern would have to be used anyway.
In marshmallow v3, an error is raised in case of
attribute
ordata_key
collision (excludingdump_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.