As I understand the docs, I could use the data_key param if the name of the field is not the same as the key name in the dict.
data_key (str) – The name of the key to get the value from when deserializing. If None, assumes the key has the same name as the field.
However, this does not work for me. The your_name field is not loaded from the dict. If I change the key to name it gets loaded and the data objects now has a key your_name. That is not what I expected based on the documentation. I am using marshmallow==2.19.2.
>>> from marshmallow import Schema, fields
>>> class FooLoad(Schema):
... name = fields.Str(data_key='your_name')
...
>>> schema_load = FooLoad()
>>> schema_load.load({'your_name': 'qwertz'})
UnmarshalResult(data={}, errors={})
>>> schema_load.load({'name': 'qwertz'})
UnmarshalResult(data={'your_name': 'qwertz'}, errors={})
marshmallow 2.x uses load_from and dump_to instead of data_key. Use those instead. The 2.x documentation is here: https://marshmallow.readthedocs.io/en/2.x-line/quickstart.html#specifying-deserialization-keys
Ok, thanks. This works.
But the docs may be a bit unintuitiv, because if you go to https://marshmallow.readthedocs.io/ the docs for version 3 are shown. However, pip currently installs version 2.19.2. Also the version the docs refer to are not really highlighted. That was what happened to me, I did not noticed that I was refering to the wrong docs.
There should be a warning at the top of the page that reads:
Warning: This document is for the latest 3.0 pre-release. For the 2.x documentation, see here.
Oh you're right. I had no banner because js was disallowed. I am using uMatrix and disallow all scripts on default. The banner is only shown if js is allowed.
@sloria About the data_key property. Now that is being used for both serialization and deserialization instead of as it says in the docs ("data_key" for deserialization and "attribute" for serialization). If data_key is "merged" for both use-cases(?) Is there no way left of configure different prop names for output/input?
When serializing and deserializing,
attribute is used to specify the attribute name in the object (if it differs from the field name)data_key is used to specify the key in the serialized dict (if it differs from the field name)That what I thought. but looking at the source code (and the behavior I am experience) on line: 471
in https://github.com/marshmallow-code/marshmallow/blob/3.0.0rc7/src/marshmallow/schema.py
it clearly looks like data_key is used also for serialization and not only deserialization
key = field_obj.data_key or attr_name. I am confused.
Both attribute and data_key are used for both serialization and deserialization. They are just at the opposite end of the chain.
Object ------------- Field ------------- Serialized dict
attribute ------------------------------- data_key
(or field name) -------------------- (or field name)
Thanks for such nice and fast replies :)!
Most helpful comment
Both
attributeanddata_keyare used for both serialization and deserialization. They are just at the opposite end of the chain.Object ------------- Field ------------- Serialized dict
attribute ------------------------------- data_key
(or field name) -------------------- (or field name)