flask_more_smorest.crud
CRUD module for automatic RESTful API generation.
This module provides the CRUDBlueprint class for creating automatic CRUD operations on SQLAlchemy models.
- class flask_more_smorest.crud.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 useschema (
type[Schema] |str|None) – Schema class or string name to usemethods (
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.
Modules
Enhanced Blueprint with automatic operationId generation. |
|
CRUD Blueprint for automatic RESTful API generation with Flask-Smorest. |
|
Query filtering utilities for Flask-Smorest CRUD operations. |