question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create a Schema containing a dict of nested Schema?

See original GitHub issue

Hi. I’ve been digging around and couldn’t find the answer to this.

Say I’ve got a model like this:

class AlbumSchema(Schema):
    year = fields.Int()

class ArtistSchema(Schema):
    name = fields.Str()
    albums = ...

I want albums to be a dict of AlbumSchema, so that ArtistSchema serializes as

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

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), which I find less intuitive, and I don’t know about a dict of Schema.

Is there any way to do it? Or a good reason not to do it?

(Question also asked on SO.)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:13
  • Comments:30 (21 by maintainers)

github_iconTop GitHub Comments

13reactions
lafrechcommented, Apr 3, 2019
class ArtistSchema(Schema):
    name = fields.Str()
    albums = fields.Dict(keys=fields.Str(), values=fields.Nested(AlbumSchema()))
13reactions
sloriacommented, Dec 30, 2017

This is released in version 3.0.0b5. Thanks everyone for your feedback!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I serialise a nested dictionary with Marshmallow?
Nested functionality. Using their example. from marshmallow import Schema, fields, pprint class UserSchema(Schema): name = fields.
Read more >
Nesting Schemas — marshmallow 3.19.0 documentation
Schemas can be nested to represent relationships between objects (e.g. foreign key relationships). For example, a Blog may have an author represented by...
Read more >
Specify nested and repeated columns in table schemas
To create a column with nested data, set the data type of the column to RECORD in the schema. A RECORD can be...
Read more >
Creating Nested Schema Using BigQuery API - Medium
Defining a schema using the BigQuery API is simple enough. To specify the column types, BigQuery provides the SchemaField method.
Read more >
Python Nested Dictionaries - W3Schools
Nested Dictionaries · Example. Create a dictionary that contain three dictionaries: myfamily = { "child1" : { "name" : "Emil", "year" : 2004...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found