How to pass parameters to a nested schema which is referred by name
See original GitHub issueI would like to refer to a schema by its name when I use it as a nested one, but still I’d like to pass custom arguments to its constructor.
For example instead of doing this:
class AuthorSchema(Schema):
books = fields.Nested(BookSchema(enveloped=True, many=True))
class Meta:
fields = ('id', 'name', 'books')
I’d like to use something like this:
class AuthorSchema(Schema):
books = fields.Nested('BookSchema', enveloped=True, many=True)
class Meta:
fields = ('id', 'name', 'books')
Please note that in this case the BookSchema
’s __init__
method is looking for a custom parameter called enveloped
.
Is there any official way that I can achieve this using only the native Nested
field?
I’m using the following versions of marshmallow:
marshmallow==2.16.3
flask-marshmallow==0.9.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Python marshmallow parameter in nested - Stack Overflow
I really don't know how to pass the parameter in the schema instance to get the SonSchema. Do you have any ideas? python...
Read more >Specify nested and repeated columns in table schemas
This page describes how to define a table schema with nested and repeated columns in BigQuery. For an overview of table schemas, see...
Read more >Structuring a complex schema — Understanding JSON ...
Complex parts of the schema can be defined in $defs with descriptive names and referenced where it's needed. This allows readers of the...
Read more >Analyze complex data types in Azure Synapse Analytics
Analyzing nested schema and arrays can involve time-consuming and complex SQL queries. Additionally, it can be difficult to rename or cast the ...
Read more >Indexing Nested Child Documents - Apache Solr
The parent with all children is referred to as a nested document or "block" and it explains some of the nomenclature of related...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
I wonder if we should instead allow for a callable, like in SQLAlchemy.
Thoughts?
There is currently not a way to pass custom arguments when the nested schema is referenced as a string. What you can do instead is subclass the schema with the custom
Meta
properties and use that schema name instead.I’m not exactly sure where
enveloped
comes from, so if this is not part of theMeta
then you might have to get a little creative with passing the arg through the constructor.It seems like this could be made a lot more convenient by adding an argument that passes schema constructor args through the nested field constructor.