How to create a Schema containing a dict of nested Schema?
See original GitHub issueHi. 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:
- Created 7 years ago
- Reactions:13
- Comments:30 (21 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is released in version 3.0.0b5. Thanks everyone for your feedback!