Skip to content

The default value specified in the field when the value is None #1656

Description

@FRiMN

I have scheme like this:

class NullProcessingSchema(Schema):
    @post_load
    def null_to_default_value(self, data: dict, **kwargs):
        return {
            key: (0 if key == 'field_1' and value is None else value)    # explicit field name
            for key, value in data.items()
        }

class ContentData(NullProcessingSchema):
    field_1 = fields.Int(allow_none=True)
    field_2 = fields.Str(allow_none=True)
    # etc...

I have many fields with different types in ContentData.
I don't want to explicitly list the field names in post_load.

Is there a way to define a default value (when None) in the field itself?
Or is it possible to get the type of a field in post_load?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions