question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

attribute used instead of data_key during deserialization

See original GitHub issue

This may be related to #748. When a data_key is provided for a field the deserialized data contains the wrong key.

import marshmallow as mm


class TestSchema(mm.Schema):
    type = mm.fields.String(attribute='_type', data_key='type')


data = {
    'type': 'value'
}

assert TestSchema().load(data) == data

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
lafrechcommented, Dec 19, 2018

Use one schema for loading and one for dumping.

class FooLoad(Schema):
    name = fields.Str()
    email = fields.Email(data_key='emailAddress')

class FooDump(Schema):
    name = fields.Str()
    email = fields.Email()

schema_dump = FooDump()
schema_dump.dump({'name': 'foo', 'email': 'x@x.org'})
# {'name': 'foo', 'email': 'x@x.org'}

schema_load = FooLoad()
schema_load.load({'name': 'foo', 'emailAddress': 'x@x.org'})
# {'name': 'foo', 'email': 'x@x.org'}
1reaction
djlambertcommented, May 2, 2018

I’ll just delete the _type key so I don’t have to update all my models. Thanks for your help! This issue can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fields — marshmallow 3.19.0 documentation
attr – The attribute/key in data to be deserialized. ... fields for a single attribute. In most cases, you should use data_key instead....
Read more >
How to handle overflow JSON or use JsonElement or ...
Learn how to handle overflow JSON or use JsonElement or JsonNode while using System.Text.Json to serialize and deserialize JSON in .NET.
Read more >
Use DataMember during deserialize but nut during serialize?
This works fine and the data is mapped to the internal property name. At some point I have to serialize this object again...
Read more >
marshmallow -- simplified object serialization | We all are data.
One common use case is to wrap data in a namespace upon serialization and unwrap the data during deserialization. from marshmallow import Schema ......
Read more >
Eloquent: API Resources - Laravel - The PHP Framework For ...
For example, you may wish to display certain attributes for a subset of ... To generate a resource class, you may use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found