flask_more_smorest.crud.crud_blueprint

CRUD Blueprint for automatic RESTful API generation with Flask-Smorest.

This module provides a Blueprint subclass that automatically generates RESTful CRUD (Create, Read, Update, Delete) endpoints for SQLAlchemy models with Marshmallow schemas.

Functions

resolve_schema(schema_candidate, ...[, ...])

Unified schema resolution for all contexts.

Classes

CRUDBlueprint(name, import_name[, model, ...])

Blueprint subclass that automatically registers CRUD routes.

CRUDConfig(name, url_prefix, import_name, ...)

Configuration object for CRUD blueprint setup.

CRUDMethod(*values)

Standard CRUD operations supported by CRUDBlueprint.

MethodConfig

Configuration for a specific CRUD method.

class flask_more_smorest.crud.crud_blueprint.CRUDMethod(*values)[source]

Standard CRUD operations supported by CRUDBlueprint.

INDEX = 'INDEX'
GET = 'GET'
POST = 'POST'
PATCH = 'PATCH'
DELETE = 'DELETE'
class flask_more_smorest.crud.crud_blueprint.MethodConfig[source]

Configuration for a specific CRUD method.

schema: type[Schema] | str
arg_schema: type[Schema] | str
admin_only: bool
public: bool
flask_more_smorest.crud.crud_blueprint.resolve_schema(schema_candidate, schema_import_path, default_schema=None, *, context='')[source]

Unified schema resolution for all contexts.

Resolves a schema reference (class, instance, or string name) to a Schema class. This is the single source of truth for schema resolution throughout the blueprint.

Parameters:
  • schema_candidate (type[Schema] | Schema | str | None) – Schema to resolve - can be: - A Schema class: returned directly - A Schema instance: returns its class - A string: imports from schema_import_path - None: returns default_schema if provided

  • schema_import_path (str) – Module path to import string schemas from

  • default_schema (type[Schema] | None) – Fallback schema if candidate is None

  • context (str) – Description for error messages (e.g., “PATCH arg_schema”)

Return type:

type[Schema]

Returns:

Resolved Schema class

Raises:
  • ValueError – If schema cannot be resolved or imported

  • TypeError – If resolved value is not a Schema subclass

Example

>>> schema_cls = resolve_schema("UserSchema", "myapp.schemas")
>>> schema_cls = resolve_schema(UserSchema, "", default_schema=BaseSchema)
class flask_more_smorest.crud.crud_blueprint.CRUDConfig(name, url_prefix, import_name, model_cls, model_name, schema_cls, schema_name, schema_import_path, model_import_path, res_id_name, res_id_param_name, methods)[source]

Configuration object for CRUD blueprint setup.

name: str
url_prefix: str
import_name: str
model_cls: type[BaseModel]
model_name: str
schema_cls: type[Schema]
schema_name: str
schema_import_path: str
model_import_path: str
res_id_name: str
res_id_param_name: str
methods: dict[CRUDMethod, MethodConfig]
__init__(name, url_prefix, import_name, model_cls, model_name, schema_cls, schema_name, schema_import_path, model_import_path, res_id_name, res_id_param_name, methods)
class flask_more_smorest.crud.crud_blueprint.CRUDBlueprint(name, import_name, model=None, schema=None, model_import_name=None, schema_import_name=None, res_id='id', res_id_param=None, methods=[CRUDMethod.INDEX, CRUDMethod.GET, CRUDMethod.POST, CRUDMethod.PATCH, CRUDMethod.DELETE], skip_methods=None, default_page_size=20, db_session=None, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_group=None)[source]

Blueprint subclass that automatically registers CRUD routes.

This class extends Flask-Smorest Blueprint to provide automatic CRUD (Create, Read, Update, Delete) operations for SQLAlchemy models. It automatically generates RESTful endpoints based on the provided model and schema configuration.

Parameters:
  • name (str) – Blueprint name (first positional arg)

  • import_name (str) – Import name (second positional arg)

  • model (type[BaseModel] | str | None) – Model class or string name to use

  • schema (type[Schema] | str | None) – Schema class or string name to use

  • methods (list[CRUDMethod] | Mapping[CRUDMethod, MethodConfig | bool]) –

    Controls which CRUD methods to enable. Can be: - List of CRUDMethod: Only these methods are enabled - Dict mapping CRUDMethod to config: All methods enabled by default,

    unless explicitly set to False or present in skip_methods. Dict values can be:

    • True: Enable with defaults

    • False: Disable this method

    • MethodConfig dict: Enable with custom configuration

  • skip_methods (list[CRUDMethod] | None) – List of CRUDMethod to explicitly disable. Applied after methods resolution. Useful when using dict-style methods to disable specific defaults.

  • *pargs – Additional keyword arguments for CRUD configuration

  • **kwargs

    Additional keyword arguments for CRUD configuration

Examples

Basic usage (all methods enabled):

from myapp.models import Product

blueprint = CRUDBlueprint(
    "products",
    __name__,
    model=Product,  # Use class (preferred)
    schema=Product.Schema,  # Auto-generated schema
)

Enable only specific methods:

blueprint = CRUDBlueprint(
    "products",
    __name__,
    model=Product,
    schema=Product.Schema,
    methods=[CRUDMethod.INDEX, CRUDMethod.GET],  # Read-only
)

Disable specific methods:

blueprint = CRUDBlueprint(
    "products",
    __name__,
    model=Product,
    schema=Product.Schema,
    skip_methods=[CRUDMethod.DELETE],  # All except delete
)

Advanced configuration (custom schemas, admin-only):

blueprint = CRUDBlueprint(
    "products",
    __name__,
    model=Product,
    schema=Product.Schema,
    methods={
        CRUDMethod.POST: {"schema": ProductCreateSchema},
        CRUDMethod.DELETE: {"admin_only": True},
        CRUDMethod.PATCH: False,  # Explicitly disable
    },
)
__init__(name, import_name, model=None, schema=None, model_import_name=None, schema_import_name=None, res_id='id', res_id_param=None, methods=[CRUDMethod.INDEX, CRUDMethod.GET, CRUDMethod.POST, CRUDMethod.PATCH, CRUDMethod.DELETE], skip_methods=None, default_page_size=20, db_session=None, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_group=None)[source]

Initialize CRUD blueprint with model and schema configuration.