Merge nested data with the same 'data_key'
See original GitHub issuefrom marshmallow import Schema, fields, EXCLUDE
from pprint import pprint
data = {
'username' : "John",
'address': {
'phone': '88000111000',
'street': 'Kolobko Str',
'home': 15,
},
}
class Address:
def __init__(self, *args, **kwargs):
self.street = kwargs.get('street')
self.home = kwargs.get('home')
class Contacts:
def __init__(self, *args, **kwargs):
self.phone = kwargs.get('phone')
class User:
def __init__(self, *args, **kwargs):
self.name = kwargs.get('name')
self.address = kwargs.get('address')
self.contacts = kwargs.get('contacts')
class AddressSchema(Schema):
street = fields.String()
home = fields.Integer()
class ContactsSchema(Schema):
phone = fields.String()
class UserSchema(Schema):
name = fields.String(data_key='username')
address = fields.Nested(AddressSchema, only=['home', 'street'], unknown=EXCLUDE)
contacts = fields.Nested(ContactsSchema, only=['phone'], data_key='address', unknown=EXCLUDE)
schema = UserSchema()
user = schema.load(data)
res = schema.dump(user)
pprint(res, indent=2)
code above show different results:
➜ python test_marshmallow/test_nested_fields.py
{'address': {'home': 15, 'street': 'Kolobko Str'}, 'username': 'John'}
➜ python test_marshmallow/test_nested_fields.py
{'address': {'phone': '88000111000'}, 'username': 'John'}
while expected:
{
'username': 'John',
'address': {
'home': 15,
'street': 'Kolobko Str',
'phone': '88000111000'
}
}
marshmallow==3.0.0b16
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Merge Object keys and nested array data - Stack Overflow
Assuming you know that all of the properties have the same length, this would probably do the trick: let obj = {}; //Put...
Read more >CDC using Merge - Databricks
Change data capture (CDC) is a type of workload where you want to merge the reported row changes from another database into your...
Read more >Reshaping Your Data with tidyr
This leads to difficult-to-read nested functions and/or choppy code. ... Function: gather(data, key, value, ..., na.rm = FALSE, convert = FALSE) Same as: ......
Read more >Report Tables - Ignition User Manual 8.1
If the table needs to display values from multiple data sources, then a Nested Query should be used to collect all the values,...
Read more >array_replace_recursive - Manual - PHP
array_replace_recursive() replaces the values of array with the same values from all the ... array_merge_recursive() - Merge one or more arrays recursively.
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
Having this raise an exception seems like it’s the correct solution, given that this is essentially a configuration error. Ideally this would be raised during creation of the schema, helping with visibility.
Quietly (and non-deterministically) doing stuff to the data going through the schema is likely to result in serious and hard to understand bugs.
I just sent a PR to raise a
ValueError
in case of duplicate: https://github.com/marshmallow-code/marshmallow/pull/992.@ehles, @edelooff, please have a look and comment if you have the time.