How to dynamically generate marshmallow schemas for SQLAlchemy models

From marshmallow-sqlalchemy recipes:

"Automatically Generating Schemas For SQLAlchemy Models It can be tedious to implement a large number of schemas if not overriding any of the generated fields as detailed above. SQLAlchemy has a hook that can be used to trigger the creation of the schemas, assigning them to the SQLAlchemy model property ".

My example using flask_sqlalchemy & marshmallow_sqlalchemy:

from flask_sqlalchemy import SQLAlchemy
from marshmallow_sqlalchemy import ModelConversionError, ModelSchema
from sqlalchemy import event
from sqlalchemy.orm import mapper


db = SQLAlchemy()


def setup_schema(Base, session):
    # Create a function which incorporates the Base and session information
    def setup_schema_fn():
        for class_ in Base._decl_class_registry.values():
            if hasattr(class_, "__tablename__"):
                if class_.__name__.endswith("Schema"):
                    raise ModelConversionError(
                        "For safety, setup_schema can not be used when a"
                        "Model class ends with 'Schema'"
                    )

                class Meta(object):
                    model = class_
                    sqla_session = session

                schema_class_name = "%sSchema" % class_.__name__

                schema_class = type(schema_class_name, (ModelSchema,), {"Meta": Meta})

                setattr(class_, "Schema", schema_class)

    return setup_schema_fn


event.listen(mapper, "after_configured", setup_schema(db.Model, db.session))

There is another example in the recipes:

https://marshmallow-sqlalchemy.readthedocs.io/en/latest/recipes.html#automatically-generating-schemas-for-sqlalchemy-models


You could create a class decorator that adds the Schema to your models:

def add_schema(cls):
    class Schema(ma.ModelSchema):
        class Meta:
            model = cls
    cls.Schema = Schema
    return cls

and then

@add_schema
class Entry(db.Model):
    ...

The schema will be available as the class attribute Entry.Schema.

The reason your original attempt fails is that marshmallow Schema classes are constructed using a custom metaclass, which inspects the namespace created from executing the class body and does its thing. When you modify the already constructed class, it is too late.

If you're unfamiliar with metaclasses in Python, read about them in the language reference. They are a tool that allows for great things and great misuse.


Some more complex types, such as enums, require additional information and dedicated field types to work properly. For example using marshmallow-enum and a decorator factory pattern it is possible to configure the model schema to accommodate enums:

from marshmallow_enum import EnumField

def add_schema(**kwgs):
    def decorator(cls): 
        class Meta:
            model = cls

        schema = type("Schema", (ma.ModelSchema,), {"Meta": Meta, **kwgs})
        cls.Schema = schema
        return cls

    return decorator

...


@add_schema(
    my_enum=EnumField(MyEnumType, by_value=True)
)
class Entry(db.Model):
    ...

Of course another way would be to make the decorator itself smarter and inspect the class before building the schema, so that it handles special cases such as enums.