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.

[question] Skip missing fields instead of default values

See original GitHub issue

I have tried to find answer myself but failed - it seems it is not supported right now.

In case if I’ve missed it: is it possible to skip missing fields instead of assigning default values during serialization?

I will try to describe it in example:

some_data = dict(
    first_name='Joe',
    age=20,
)


class TestSchema(Schema):
    first_name = String()
    family_name = String()
    age = Integer()


schema = TestSchema()
print(schema.dump(some_data).data)

Current result: OrderedDict([('first_name', u'Joe'), ('family_name', ''), ('age', 20)]) Desired result: OrderedDict([('first_name', u'Joe'), ('age', 20)])

Of course it is possible to filter the result afterwards. Although it is quite tricky due to the different default values (i.e. for strings, integers) but possible and I’ve already done it.

I am just curious if I’ve missed some core functionality.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Behostoncommented, Jan 31, 2017
  1. Recursive
class NotNoneSchemaBase:
    @post_dump
    def clear_none(self, data):
        result = {}
        for k, v in data.items():
            if v is None:
                continue
            elif isinstance(v, dict):
                result[k] = self.clear_none(v)
            else:
                result[k] = v
        return result
1reaction
sloriacommented, May 5, 2015

An update: skipping missing values is now the default behavior in marshmallow 2.0.0: https://marshmallow.readthedocs.org/en/latest/upgrading.html#default-values

For this reason, I think I’m going to go ahead and deprecate the skip_missing option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

set default values for fields when using skip logic in qualtrics
If a user selects '1' for Yes, I know how to easily skip the next 5 questions in Qualtrics. But, I don't just...
Read more >
Checking if a scalar field is truly missing (not set to default ...
I know there is a checkField function that tells whether a field is missing but it does not distinguish between a missing field...
Read more >
Set default values for blank fields in Zaps - Zapier
Set default values for blank fields in Zaps · 1. Add Formatter to your Zap · 2. Set up the Default Value text...
Read more >
Default Value for Skipped Question - Support - ODK Forum
i tried using a default value for skipped questions but default value disappears when i skip and unskip the question it replaces the...
Read more >
Expressions - Apache FreeMarker Manual
Default value operator; Missing value test operator ... Note that the character sequence ${ and #{ (and rarely [= instead, depending on 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