Comments (3)
I don't see an explicit test for this. @sloria was this intended?
from marshmallow.
from_dict
lets you use field names that would otherwise not be valid class attributes. The observed behavior is consistent with using dotted keys for data_key
and attribute
.
from marshmallow import Schema, fields
Test = Schema.from_dict({'foo.bar': fields.Str()})
schema = Test()
obj = schema.load({'foo.bar': 'baz'})
print('from_dict', 'load', obj)
data = schema.dump(obj)
print('from_dict', 'dump', data)
class Test(Schema):
foo_bar = fields.Str(data_key='foo.bar', attribute='foo.bar')
schema = Test()
obj = schema.load({'foo.bar': 'baz'})
print('Schema', 'load', obj)
data = schema.dump(obj)
print('Schema', 'dump', data)
from_dict load {'foo': {'bar': 'baz'}}
from_dict dump {'foo.bar': 'baz'}
Schema load {'foo': {'bar': 'baz'}}
Schema dump {'foo.bar': 'baz'}
This behavior is not documented for attribute
thought. It appears to be a side effect of performing dotted name resolution in get_value
at load time.
A workaround would be to explicitly define an attribute
for the field without dots so that no nesting occurs.
from marshmallow import Schema, fields
Test = Schema.from_dict({'foo.bar': fields.Str(attribute='foo_bar')})
schema = Test()
obj = schema.load({'foo.bar': 'baz'})
print('from_dict', 'load', obj)
data = schema.dump(obj)
print('from_dict', 'dump', data)
class Test(Schema):
foo_bar = fields.Str(data_key='foo.bar', attribute='foo_bar')
schema = Test()
obj = schema.load({'foo.bar': 'baz'})
print('Schema', 'load', obj)
data = schema.dump(obj)
print('Schema', 'dump', data)
from_dict load {'foo_bar': 'baz'}
from_dict dump {'foo.bar': 'baz'}
Schema load {'foo_bar': 'baz'}
Schema dump {'foo.bar': 'baz'}
from marshmallow.
This is an intentional behavior and it is covered with tests for attribute
. See #450.
It is tempting to start adding ways to opt out of this behavior, but I don't think it is actually necessary to support arbitrary key structures in deserialized objects. If something is consuming the data and dictating the key structure, it should be consuming dumped data. Otherwise the code can conform to the default output or customize it with enveloping.
We should update the docs for attribute
and from_dict
to advertise this behavior.
from marshmallow.
Related Issues (20)
- Feature request: dynamic field type HOT 1
- data_key in Field does not work as load_from in version 2.x HOT 2
- Are nested options applied correctly? HOT 1
- [RFC] Add BigInt, SmallInt,... fields. HOT 3
- RecursionError: maximum recursion depth exceeded while calling a Python object HOT 1
- Question - Is it possible to ignore fields on dump? HOT 4
- sdist is missing `tox.ini` HOT 4
- Validation does not occur on serialization documentation issue HOT 2
- The data_key argument for one or more fields collides with another field's name or data_key argument. HOT 2
- Serialize data objects in Schema HOT 1
- Raise error for valid e-mail
- Is this safer than Pickle? HOT 1
- Wrong value in __call__() function of validate.Validator HOT 1
- Validation of required nested field is not consistent with previous version
- Create raw list Schema HOT 1
- The function decorated by the "post_load" decorator will undergo changes in parameter types as the function name changes. HOT 1
- Deprecate __version__ and related attributes
- Address deprecation warnings in tests HOT 1
- URL validation fails if URL is missing user field HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from marshmallow.