flask_more_smorest.sqla.schema

Schema generation for SQLAlchemy models.

This module provides BaseSchema for Marshmallow schemas with automatic view_args injection and schema generation utilities.

Functions

create_model_schema(model_cls, *[, db_session])

Create a Marshmallow schema for a SQLAlchemy model class.

Classes

BaseModelConverter([schema_cls])

Model converter for SQLAlchemy models with enhanced relationship handling.

BaseSchema(*args, **kwargs)

Base schema for all Marshmallow schemas.

class flask_more_smorest.sqla.schema.BaseSchema(*args, **kwargs)[source]

Base schema for all Marshmallow schemas.

This schema extends SQLAlchemyAutoSchema with automatic view_args injection for URL parameters and adds an is_writable field for permission checking.

is_writable

Read-only boolean field indicating if current user can write to the resource

Example

>>> class ArticleSchema(BaseSchema):
...     class Meta:
...         model = Article
...         include_relationships = True
pre_load(data, **kwargs)[source]

Pre-load hook to handle UUID conversion and view_args injection.

Automatically injects URL parameters from Flask’s request.view_args into the data being loaded, allowing schemas to access route parameters.

Parameters:
  • data (dict[str, str | int | float | bool]) – The input data dictionary

  • **kwargs (Any) – Additional keyword arguments from Marshmallow

Return type:

dict[str, str | int | float | bool]

Returns:

The modified data dictionary with view_args injected

Example

Given a route /articles/<uuid:article_id> and a schema field article_id, the article_id from the URL will be automatically injected into the data if not already present.

opts: Opts = <marshmallow_sqlalchemy.schema.SQLAlchemyAutoSchemaOpts object>
instance: _ModelType | None
class flask_more_smorest.sqla.schema.BaseModelConverter(schema_cls=None)[source]

Model converter for SQLAlchemy models with enhanced relationship handling.

This converter extends marshmallow_sqlalchemy’s ModelConverter to provide better handling of relationships, particularly around nullable constraints and dump_only settings.

flask_more_smorest.sqla.schema.create_model_schema(model_cls, *, db_session=None)[source]

Create a Marshmallow schema for a SQLAlchemy model class.

This function generates a schema class with sensible defaults for CRUD operations, including relationship handling and automatic timestamp field management.

Parameters:
  • model_cls (type) – SQLAlchemy model class to generate schema for

  • db_session (Any) – SQLAlchemy session to use (if None, uses model_cls’s session)

Return type:

type[BaseSchema]

Returns:

Generated BaseSchema subclass for the model

Example

>>> from flask_more_smorest.sqla import BaseModel
>>> class Article(BaseModel):
...     title: Mapped[str] = mapped_column(sa.String(200))
>>>
>>> ArticleSchema = create_model_schema(Article)
>>> schema = ArticleSchema()