RFC: replace load_from and dump_to with a single parameter
See original GitHub issueSee discussion in https://github.com/marshmallow-code/marshmallow/pull/714.
In the general case, dump/load is symmetric and if you have a model field that is exposed with another name, you use the API name in the schema, and specify the model name as attribute
.
class MySchema(Schema):
ApiName = fields.String(attribute='model_name')
dump_to
and load_from
introduce asymmetry, allowing to specify a different key for dump and load:
class MySchema(Schema):
model_name = fields.String(load_from='api_load_name', dump_to='api_dump_name')
If you want to reproduce use case 1 using these, you need to specify both load_from
and dump_to
and make sure they match:
class MySchema(Schema):
model_name = fields.String(load_from='api_name', dump_to='api_name')
The flexibility brought by having both load_from
and dump_to
comes at a price: complexity for the user but also in the code (marshmallow and related libs like webargs, apispec…), potential undefined cases for some weird combinations…
Assuming symmetry is not required, a single parameter should be enough. There is however a limitation with attribute
. It can’t be used for APIs where the keys are invalid Python variable names:
class MySchema(Schema):
ApiKey = fields.String(attribute='apikey')
weird-key = fields.String(attribute='weird_key') # syntax error
Currently, load_from
and dump_to
can be used for this as shown above.
The proposal here is to introduce a new parameter to unite them all. Let’s call it key
for now, short of a better name:
class MySchema(Schema):
apikey = fields.Int(key='ApiKey')
weird_key = fields.Int(key='weird-key')
If we do that, then there is no point in attribute
anymore (except backward compatibility). So we’d remove attribute
, dump_to
and load_from
.
Any objection to this?
Any suggestion for a better name?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:10 (7 by maintainers)
Top GitHub Comments
For anyone interested in different keys for serialization/deserialization - here is hack for marshmallow>=3.0.0b8 https://github.com/vgavro/requests-client/blob/0d88c8f907ae2b8e9f77ae2c7144741032acc0b8/requests_client/schemas.py#L90
I was reading the discussion on PR #174. Just to add some context and see if I understand well (the doc is not always very clear):
We have a
Python Object
that by means of anSchema
instance we serialize (dump
) into aSerialized Representation
, that we can take the backwards process and deserialize (load
) into aPython Object
.A
Schema
haveFields
that knows how the data can be de/serialized. ThatFields
can take anattribute
from aPython Object
and serialize it into akey/name
in a map of aSerialized Representation
, and back.Some comments answering the questions:
attribute
should be kept to decouple the schemas from your Python models/objects. Theattribute
parameter represents the name of the attribute of thePython Object
where the data should be serialized from / deserialize to. If not present (None
) Marshmallow assumes that the object attribute have the same name as the schema field. Same current behavior.load_from
,dump_to
arguments should be consolidated into one that represents thekey/name
on theSerialized Representation
, with the mirror behavior ofattribute
but for theSerialized Representation
side: if is not present (None
) Marshmallow should assume that thekey/name
is he same as the schema field name.If you still need different values in
load_from
anddump_to
what you need is two different schemasA
key
name could be too terse without a clear doc on what it represents. Other names could be:key_name
,name
(JSON),field
(OpenAPI),field_name
,field_key
, etc.