Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshmallow Field not loading default value

I have a Marshmallow Schema defined as:

class MySchema:
    myfield = fields.Str(required=False, default=“value”)

When I do:

s = MySchema().load({})

I would expect the return to be: {‘myfield’:’value’}

But I am getting {} in return.

Is there anything am I missing?

Edit:

I’m using marshmallow 3.11 due to project limitations. I can’t upgrade to 3.15. Tried with 3.15 and it is working as expected.

like image 627
Rob Avatar asked May 11 '26 18:05

Rob


1 Answers

Until marshmallow 3.12:

  • default: value to use by default when dumping (serializing)
  • missing: value to use by default when loading (deserializing)

Since marshmallow 3.13:

  • dump_default: value to use by default when dumping (serializing)
  • load_default: value to use by default when loading (deserializing)

default / missing still behave the same in marshmallow 3.13+ but issue a deprecation warning.

https://marshmallow.readthedocs.io/en/stable/changelog.html#id4

like image 61
Jérôme Avatar answered May 14 '26 09:05

Jérôme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!