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 a Marshmallow schema for a SQLAlchemy model class. |
Classes
|
Model converter for SQLAlchemy models with enhanced relationship handling. |
|
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:
- Return type:
- 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>
- 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:
- Return type:
- 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()