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 pass parameters to a nested schema which is referred by name

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sloriacommented, Sep 8, 2019

I wonder if we should instead allow for a callable, like in SQLAlchemy.

class AuthorSchema(Schema):
    books = fields.List(fields.Nested(lambda: BookSchema(enveloped=True)))

Thoughts?

1reaction
deckar01commented, Feb 21, 2019

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.

class NestedBookSchema(BookSchema):
    class Meta:
        enveloped = True

...
    books = fields.Nested('NestedBookSchema', many=True)

I’m not exactly sure where enveloped comes from, so if this is not part of the Meta 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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