Marshmallow: Dict of nested Schema

10,119

This is not possible right now, but it is a feature request:

and it has been worked on already:

2017-12-31: This feature was added to Marshmallow 3.0.0b5 (https://github.com/marshmallow-code/marshmallow/pull/700).

Share:
10,119
Jérôme
Author by

Jérôme

Breaking second law of thermodynamics? I don't mind. But PEP8... Wow, that's serious stuff.

Updated on June 25, 2022

Comments

  • Jérôme
    Jérôme almost 2 years

    I'm wondering how to serialize a dict of nested Schema.

    Naively, I would expect syntaxes like this to work:

    fields.List(Schema)
    fields.Dict(Schema)
    

    or maybe

    fields.List(fields.Nested(Schema))
    fields.Dict(fields.Nested(Schema))
    

    Serializing a list of Schema can be achieved through Nested(Schema, many=True), but I don't know about a dict of Schema.

    Assume, for example's sake, that my object is defined like this:

    from marshmallow import Schema, fields, pprint
    
    class AlbumSchema(Schema):
        year = fields.Int()
    
    class ArtistSchema(Schema):
        name = fields.Str()
    
        # What should I write, here?
    
        # This won't work
        albums = fields.Nested(AlbumSchema(), many=True)
    
        # If I write this, AlbumSchema is ignored, so this is equivalent to
        albums = fields.Dict(AlbumSchema(), many=True)
        # this, which is not satisfying (AlbumSchema unused)
        albums = fields.Dict()
        # This is not the way either
        albums = fields.Dict(fields.Nested(AlbumSchema))
    
    
    album_1 = dict(year=1971)
    album_2 = dict(year=1970)
    bowie = dict(name='David Bowie',
                 albums={
                     'Hunky Dory': album_1,
                     'The Man Who Sold the World': album_2
                 }
            )
    
    schema = ArtistSchema()
    result = schema.dump(bowie)
    pprint(result.data, indent=2)
    

    I expect my object to be serialized as

    { 'albums': { 'Hunky Dory': {'year': 1971},
                  'The Man Who Sold the World': {'year': 1970}},
      'name': 'David Bowie'}
    

    (Question also discussed on GitHub.)