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.

Is it possible to dynamically nest other schemas?

See original GitHub issue

I have a MySQL table that has MANY relationships (about 50) and I would like to avoid:

  • Include all relationships in nested items
  • Write schemas for each case, depending on which relationship to nest

I tried setting the nested items in the WebsiteSchema constructor, setting it in the include_items() method, but nothing works, the items property only exists if it has been defined completely in the class definition. I tried searching in the docs and Google but I cannot find how to do this, I have a hunch I should be using the OPTIONS_CLASS but I don’t quite understand how it works.

Code showing what I tried:

from components.ma import ma
from schemas.item import ItemSchema

class WebsiteSchema(ma.SQLAlchemyAutoSchema):
    # This line works obviously
    # items = ma.Nested(ItemSchema, many=True)
    
    items = None

    # Not working
    def __init__(self, **kwargs):
        self.items = ma.Nested(ItemSchema, many=True)
        super().__init__(**kwargs)

    # Not working
    def include_items(self):
        self.items = ma.Nested(ItemSchema, many=True)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
NaturalBornCampercommented, Jun 3, 2020

Thanks a million! from_dict with the extra Python theory I didn’t know about will be of tremendous help!

1reaction
sloriacommented, Jun 3, 2020

Oh, that was haste on my part. Since it looks like you’re using flask-marshmallow, then you can access the fields from ma. I also fixed the List(Nested(...)) usage. I updated my code snippet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongoose schema with dynamic nested Object - Stack Overflow
I use something like this. // Importing the Users Mongoose Scheme var User = require('../app/models/user'); var Feed = require('.
Read more >
Validation for Dynamically Nested Schema doesnt seem to work
Another option is to dynamically create the marshmallow serializer with Schema.from_dict if the object shape isn't well-known ahead of time.
Read more >
A dynamic schema test for nested & repeated BigQuery fields
Our goal is to write a generalizable schema test called not_null_nr that finds null records in any kind of nested/repeated data at any...
Read more >
Dynamic schemas - DGS Framework - Netflix Open Source
Dynamic schemas. We strongly recommend primarily using schema-first development. Most DGSs have a schema file and use the declarative, annotation-based ...
Read more >
Elasticsearch: Working With Dynamic Schemas the Right Way
When documents have a nested JSON schema, Elasticsearch's dynamic ... Next, you're able to assign different properties to the mutation ...
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