API Reference¶
-
marshmallow_sqlalchemy.
fields_for_model
=func(...)¶ Generate a dict of field_name:
marshmallow.fields.Field
pairs for the given model.Parameters: - model – The SQLAlchemy model
- include_fk (bool) – Whether to include foreign key fields in the output.
Returns: dict of field_name: Field instance pairs
-
marshmallow_sqlalchemy.
property2field
=func(...)¶ Convert a SQLAlchemy
Property
to a field instance or class.Parameters: Returns: A
marshmallow.fields.Field
class or instance.
-
marshmallow_sqlalchemy.
column2field
=func(...)¶ Convert a SQLAlchemy
Column
to a field instance or class.Parameters: Returns: A
marshmallow.fields.Field
class or instance.
-
marshmallow_sqlalchemy.
field_for
=func(...)¶ Convert a property for a mapped SQLAlchemy class to a marshmallow
Field
. Example:date_created = field_for(Author, 'date_created', dump_only=True) author = field_for(Book, 'author')
Parameters: - model (type) – A SQLAlchemy mapped class.
- property_name (str) – The name of the property to convert.
- kwargs – Extra keyword arguments to pass to
property2field
Returns: A
marshmallow.fields.Field
class or instance.
-
class
marshmallow_sqlalchemy.
TableSchema
(only=(), exclude=(), prefix='', strict=None, many=False, context=None, load_only=(), dump_only=(), partial=False)[source]¶ Base class for SQLAlchemy model-based Schemas.
Example:
from marshmallow_sqlalchemy import TableSchema from mymodels import engine, users class UserSchema(TableSchema): class Meta: table = users schema = UserSchema() select = users.select().limit(1) user = engine.execute(select).fetchone() serialized = schema.dump(user).data
-
OPTIONS_CLASS
¶ alias of
TableSchemaOpts
-
-
class
marshmallow_sqlalchemy.
ModelSchema
(*args, **kwargs)[source]¶ Base class for SQLAlchemy model-based Schemas.
Example:
from marshmallow_sqlalchemy import ModelSchema from mymodels import User, session class UserSchema(ModelSchema): class Meta: model = User schema = UserSchema() user = schema.load({'name': 'Bill'}, session=session) existing_user = schema.load({'name': 'Bill'}, instance=User.query.first())
Parameters: - session – Optional SQLAlchemy session; may be overridden in
load.
- instance – Optional existing instance to modify; may be overridden in
load
.
-
OPTIONS_CLASS
¶ alias of
ModelSchemaOpts
-
load
(data, session=None, instance=None, *args, **kwargs)[source]¶ Deserialize data to internal representation.
Parameters: - session – Optional SQLAlchemy session.
- instance – Optional existing instance to modify.
-
make_instance
(data)[source]¶ Deserialize data to an instance of the model. Update an existing row if specified in
self.instance
or loaded by primary key(s) in the data; else create a new row.Parameters: data – Data to deserialize.
-
validate
(data, session=None, *args, **kwargs)[source]¶ Validate
data
against the schema, returning a dictionary of validation errors.Parameters: - data (dict) – The data to validate.
- many (bool) – Whether to validate
data
as a collection. IfNone
, the value forself.many
is used. - partial (bool|tuple) – Whether to ignore missing fields. If
None
, the value forself.partial
is used. If its value is an iterable, only missing fields listed in that iterable will be ignored.
Returns: A dictionary of validation errors.
Return type: New in version 1.1.0.
- session – Optional SQLAlchemy session; may be overridden in
-
class
marshmallow_sqlalchemy.
TableSchemaOpts
(meta, *args, **kwargs)[source]¶ Options class for
TableSchema
. Adds the following options:table
: The SQLAlchemy table to generate theSchema
from (required).model_converter
:ModelConverter
class to use for converting the SQLAlchemy table to- marshmallow fields.
include_fk
: Whether to include foreign fields; defaults toFalse
.
-
class
marshmallow_sqlalchemy.
ModelSchemaOpts
(meta, *args, **kwargs)[source]¶ Options class for
ModelSchema
. Adds the following options:model
: The SQLAlchemy model to generate theSchema
from (required).sqla_session
: SQLAlchemy session to be used for deserialization. This is optional; you- can also pass a session to the Schema’s
load
method.
model_converter
:ModelConverter
class to use for converting the SQLAlchemy model to- marshmallow fields.
include_fk
: Whether to include foreign fields; defaults toFalse
.