Coder Social home page Coder Social logo

Comments (11)

sloria avatar sloria commented on September 28, 2024 1

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.

from marshmallow.

Behoston avatar Behoston commented on September 28, 2024 1
  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

from marshmallow.

sloria avatar sloria commented on September 28, 2024

There is currently no way to skip missing fields during serialization. One simple feature addition would be to allow fields to default to the fields.missing sentinel value, which would instruct the Schema to skip serialization if the value is missing.

class TestSchema(Schema):
    first_name = String()
    family_name = String()
    age = Integer(default=fields.missing)

Would this meet your use case?

from marshmallow.

malexer avatar malexer commented on September 28, 2024

Yep, that's a good point which I've missed - currently I am assigning None to default and then filtering is performed manually. Thanks!

from marshmallow.

sloria avatar sloria commented on September 28, 2024

@malexer The above code snippet was strictly hypothetical and will not work (at the time of this posting). It was merely a proposal so I could get feedback. If you think the above is a good solution, I can move forward with implementing it.

Reopening.

from marshmallow.

malexer avatar malexer commented on September 28, 2024

This solution is quite good but as for me it is better to add some option on a schema level (class Meta) - the similar way as it is done in schematics with serialize_when_none option.

My case: I have a set of required fields and optional ones. For optional I need to avoid having any default values in case if no such fields are found in the source data. This will need to set default=fields.missing for every such optional field.

Hope this will help you to come out with a nice solution!

from marshmallow.

sloria avatar sloria commented on September 28, 2024

I agree that a schema-level option might be useful.. I'll see what I can do with this in the next few days.

from marshmallow.

sloria avatar sloria commented on September 28, 2024

This behavior has been implemented via the skip_missing class Meta option.

from marshmallow.

Behoston avatar Behoston commented on September 28, 2024
    middle_name = fields.String(none=False, required=False)

generates

{'middle_name': None}

from

class C:
    pass
c = C()
c.middle_name = None

But I want {}

from marshmallow.

maximkulkin avatar maximkulkin commented on September 28, 2024

I see several ways to fix that:

  1. Use marshmallow.fields.Function like this for occasional fields that require to be removed:
import marshmallow as m
import marshmallow.feilds as mf
from marshmallow.utils import missing

class MySchema(m.Schema):
    my_field = mf.Function(lambda o: o.my_field or missing)
  1. Use your own post_dump filter to remove all None values:
class MySchema(m.Schema):
    my_field = mf.String()
    @m.post_dump
    def remove_nones(self, data):
        return {k: v for k, v in data.iteritems() if v is not None}
  1. Make your own wrapper field types to accomodate for that:
class MyString(mf.String):
    def _serialize(self, value, attr, obj):
        result = super(MyString, self)._serialize(value, attr, obj)
        return result if result is not None else missing

class MySchema(m.Schema):
    my_field = MyString()

from marshmallow.

maximkulkin avatar maximkulkin commented on September 28, 2024

Yeah, what I meant for post_dump case is that all schemas (that we care about) have this post_dump hook, so there is no need for recursion.

from marshmallow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.