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.

Forcing `None` on load and skipping `None` on dump

See original GitHub issue

I’m using 2.0.0rc2 to validate input data on HTTP requests and to load SQLAlchemy models to JSON on HTTP responses. And i’ve stumbled upon 2 problems:

First, while loading data from JSON on HTTP PUT request, i want to populate all missing fields as None, to correctly overwrite data in SQLAlchemy. Right now i’m using following code:

for name, field in schema.fields.iteritems():
    if field.missing == ma.missing:
        schema.fields[name].missing = None

It works, but i suppose it’s bugged since i’m messing with marshmallow.Field instance attached to Schema class. And after disposing Schema instance all fields we patched will stuck with new missing instead of default one.

Second, while dumping data from SQLAlchemy to JSON all missing fields are resolved as None, and JSON populated with {"key": null, } data. It’s unwanted behaviour and i’m cleaning them on post_dump trigger.

@post_dump
def clean_missing(self, data):
    for key in filter(lambda key: data[key] is None, data):
        data.pop(key)
    return data

Same as previous, it’s working but includes creating some BaseSchema class witch passes this logic to all inherited classes.

I’ve searched documentation for while, and didn’t find any correct way to swap this behaviours i.e. skip fields on dumping and populate fields with None on loading. Am I missing something or marshmallow don’t provide such functions?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
sloriacommented, Sep 24, 2015

What is wrong with creating a BaseSchema? This is a common usage pattern with marshmallow. You’ll often want shared behavior across all your schemas.

You can use the newly-introduced on_bind_field hook to override the missing attribute. So your BaseSchema would look something like:

from marshmallow import Schema, fields, pre_load, post_dump, missing

class BaseSchema(Schema):

    def on_bind_field(self, field_name, field_obj):
        # Override default missing attribute so
        # that missing values deserialize to None
        if field_obj.missing == missing:
            field_obj.missing = None
            field_obj.allow_none = True

    @post_dump 
    def clean_missing(self, data):
        ret = data.copy()
        for key in filter(lambda key: data[key] is None, data):
            del ret[key]
        return ret


class MySchema(BaseSchema):
    foo = fields.Field()
    bar = fields.Field()

s = MySchema()
s.load({'bar': 42}).data  # {'bar': 42, 'foo': None}
s.dump({'foo': None, 'bar': 42}).data  # {'bar': 42}
1reaction
cwisecarvercommented, Oct 30, 2015

Is there a way to pass additional arguments to nested schemas when they’re ‘self’?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Forcing `None` on load and skipping `None` on dump
To force None on load, you can use the missing parameter: missing – Default deserialization value for the field if the field is...
Read more >
Java dump - IBM
If your application hangs, you can trigger the generation of a Java dump by sending a SIGQUIT signal ( kill -3 ) to...
Read more >
Oracle Data Pump Import
Oracle Data Pump Import is a utility for loading an Oracle export dump file set into a target system. An export dump file...
Read more >
API Reference — marshmallow 2.21.0 documentation
dump_only (bool) – If True skip this field during deserialization, otherwise its value will be present in the deserialized object. In the context...
Read more >
Python Tutorial: json.dump(s) & json.load(s) - 2021
There are two ways of reading in (load/loads) the following json file, in.json: {"alpha": 1, "beta": 2}. string: import json io = open("in.json","r")...
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