Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshmallow: Dict of nested Schema

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.)

like image 977
Jérôme Avatar asked Jun 27 '16 08:06

Jérôme


People also ask

What is a nested schema?

Schemas can be nested to represent relationships between objects (e.g. foreign key relationships). For example, a Blog may have an author represented by a User object. import datetime as dt class User: def __init__(self, name, email): self.

What are marshmallow schemas?

In short, marshmallow schemas can be used to: Validate input data. Deserialize input data to app-level objects. Serialize app-level objects to primitive Python types. The serialized objects can then be rendered to standard formats such as JSON for use in an HTTP API.


1 Answers

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

  • https://github.com/marshmallow-code/marshmallow/issues/483
  • https://github.com/marshmallow-code/marshmallow/issues/496

and it has been worked on already:

  • https://github.com/marshmallow-code/marshmallow/compare/dev...deckar01:483-structured-dict

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

like image 190
Jérôme Avatar answered Oct 07 '22 15:10

Jérôme