Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't fill empty fields with default value #1765

Closed
thmsklngr opened this issue Mar 17, 2021 · 5 comments
Closed

Can't fill empty fields with default value #1765

thmsklngr opened this issue Mar 17, 2021 · 5 comments

Comments

@thmsklngr
Copy link

Hi,
I'm currently out of ideas. I try to get rid of None's in my schema, but nothing seems to work.

Here's the schema:

class ContactSchema(Schema):
    """Scheme for path `contacts`."""
    links = fields.List(fields.Nested(LinksSchema))
    contact_id = fields.Str(data_key='contactId')
    name = fields.Str()
    email = fields.Str(data_key='emailAddress')
    phone = fields.Str(
        data_key='phoneNumber',
        default='(no number available)',
        missing='(no number available)',
        allow_none=True
    )
    archived = fields.Boolean()

Regardless what I try, neither the value of missing or default it taken. I also tried to use only either default or missing. If I set allow_none=True the schema is validated in any case, but the field phone is unset or None. marshmallow is 3.10.0

Any ideas on how to solve this? According the docs this is supposed to work, or am I wrong?

Regards, Thomas

@thmsklngr
Copy link
Author

Update: solved my issue by adding a post_load activity:

class ContactSchema(Schema):
    """Scheme for path `contacts`."""
    links = fields.List(fields.Nested(LinksSchema))
    contact_id = fields.Str(data_key='contactId')
    name = fields.Str()
    email = fields.Str(data_key='emailAddress')
    phone = fields.Str(
        data_key='phoneNumber',
        missing='(no number available)',
        default='(no number available)',
        allow_none=True
    )
    archived = fields.Boolean()
    
    @post_load
    def no_number(self, item, *args, **kwargs):
        if not item['phone'] or item['phone'] == '':
            item['phone'] = '(no number available)'
        return item

Still open is the question why default or missing is not handled at all, when allow_none is set to False.

@sloria
Copy link
Member

sloria commented Mar 18, 2021

The missing value is only used if the key is actually missing from the input data. None is not considered missing.

Using post_load as you have is a good solution.

I proposed an API for customizing the values that are considered "missing" in #1381 . But that will require some thought...I'm still not sure it's a good idea.

@sloria sloria closed this as completed Mar 18, 2021
@thmsklngr
Copy link
Author

missing is just half of the truth, I added it here just for my interest. In my case phoneNumber is coming with my JSON anyway, so from my point of view the value of default should take place if phoneNumber is empty - but it actually does not work. I can get the deserialization working only, whenever I add allow_none=True.
Am I getting something wrong here or shouldn't default be in place if a key does not have a value?

@lafrech
Copy link
Member

lafrech commented Mar 18, 2021

Just to be sure it is clear. missing is for deserialization, default for serialization.

@thmsklngr
Copy link
Author

Ahaa! Got it, I just read the docs a thousand times, but I did not stumble over that parts, for any unknown reason. Thx, sorry for the inconveniences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants