Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support object polymorphism #79

Closed
abendebury opened this issue Aug 16, 2016 · 3 comments
Closed

Support object polymorphism #79

abendebury opened this issue Aug 16, 2016 · 3 comments

Comments

@abendebury
Copy link

In my application, I have an API endpoint like so:

/api/activities/

In my database, I have several different types of Activity models, all inheriting from a common class. I made a schema like so:

class ActivitySchema(ModelSchema):
    class Meta:
        model = models.Activity

And then have flask-restful handle the api endpoint itself, querying the database and passing the objects to marshmallow-alchemy.

However, marshmallow-sqlalchemy serializes the objects as if they were all of the base Activity type. It would be nice if there was some support like this:

class ActivitySchema(ModelSchema):
    class Meta:
        model = models.Activity
        generate_polymorphic_schemas = True

Which would use sqlalchemy's inspection to view all polymorpic subclasses of Meta.model, store them on ActivitySchema, and then when serializing/deserializing use the appropriate class.

Thoughts?

If this is a desirable feature, I can try to make a PR implementing such functionality.

@dolfandringa
Copy link

@abendebury: any chance that you got to do that PR?

@abendebury
Copy link
Author

Sorry, I have not been able to do so.

@sloria
Copy link
Member

sloria commented Feb 10, 2020

Sorry for the long delay, but I recommend using https://github.com/marshmallow-code/marshmallow-oneofschema for this. It works well for serializing based on a discriminator column.

@sloria sloria closed this as completed Feb 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants