Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'marshmallow-sqlalchemy' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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 = TableSchemaOpts
class ModelSchema(with_metaclass(ModelSchemaMeta, ma.Schema)):
"""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())
class ModelSchemaMeta(SchemaMeta):
@classmethod
def get_fields(mcs, converter, opts, base_fields, dict_cls):
if opts.model is not None:
return converter.fields_for_model(
opts.model,
fields=opts.fields,
exclude=opts.exclude,
include_fk=opts.include_fk,
base_fields=base_fields,
dict_cls=dict_cls,
)
return dict_cls()
class TableSchema(with_metaclass(TableSchemaMeta, ma.Schema)):
"""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()
def get_declared_fields(mcs, klass, cls_fields, inherited_fields, dict_cls):
"""Updates declared fields with fields converted from the SQLAlchemy model
passed as the `model` class Meta option.
"""
declared_fields = dict_cls()
opts = klass.opts
Converter = opts.model_converter
converter = Converter(schema_cls=klass)
base_fields = super(SchemaMeta, mcs).get_declared_fields(
klass, cls_fields, inherited_fields, dict_cls
)
declared_fields = mcs.get_fields(converter, opts, base_fields, dict_cls)
declared_fields.update(base_fields)
return declared_fields
class TableSchemaMeta(SchemaMeta):
@classmethod
def get_fields(mcs, converter, opts, base_fields, dict_cls):
if opts.table is not None:
return converter.fields_for_table(
opts.table,
fields=opts.fields,
exclude=opts.exclude,
include_fk=opts.include_fk,
base_fields=base_fields,
dict_cls=dict_cls,
)
return dict_cls()
class ModelSchemaMeta(SchemaMeta):
@classmethod
def get_fields(mcs, converter, opts, base_fields, dict_cls):
if opts.model is not None:
return converter.fields_for_model(
opts.model,
fields=opts.fields,
exclude=opts.exclude,
include_fk=opts.include_fk,
base_fields=base_fields,
dict_cls=dict_cls,
)
return dict_cls()
class TableSchema(with_metaclass(TableSchemaMeta, ma.Schema)):
declared_fields = dict_cls()
opts = klass.opts
Converter = opts.model_converter
converter = Converter(schema_cls=klass)
base_fields = super(SchemaMeta, mcs).get_declared_fields(
klass, cls_fields, inherited_fields, dict_cls
)
declared_fields = mcs.get_fields(converter, opts, base_fields, dict_cls)
declared_fields.update(base_fields)
return declared_fields
@classmethod
def get_fields(mcs, converter, base_fields, opts):
pass
class TableSchemaMeta(SchemaMeta):
@classmethod
def get_fields(mcs, converter, opts, base_fields, dict_cls):
if opts.table is not None:
return converter.fields_for_table(
opts.table,
fields=opts.fields,
exclude=opts.exclude,
include_fk=opts.include_fk,
base_fields=base_fields,
dict_cls=dict_cls,
)
return dict_cls()
class ModelSchemaMeta(SchemaMeta):
def get_instance(self, data):
"""Retrieve an existing record by primary key(s)."""
props = get_primary_keys(self.opts.model)
filters = {
prop.key: data.get(prop.key)
for prop in props
}
if None not in filters.values():
return self.session.query(
self.opts.model
).filter_by(
**filters
).first()
return None
def __init__(self, meta):
super(ModelSchemaOpts, self).__init__(meta)
self.model = getattr(meta, 'model', None)
self.sqla_session = getattr(meta, 'sqla_session', None)
self.model_converter = getattr(meta, 'model_converter', ModelConverter)
self.include_fk = getattr(meta, 'include_fk', False)
def validate(self, data, session=None, *args, **kwargs):
self.session = session or self.session
if not self.session:
raise ValueError('Validation requires a session')
return super(ModelSchema, self).validate(data, *args, **kwargs)
def __init__(self, meta):
super(ModelSchemaOpts, self).__init__(meta)
self.model = getattr(meta, 'model', None)
self.sqla_session = getattr(meta, 'sqla_session', None)
self.model_converter = getattr(meta, 'model_converter', ModelConverter)
self.include_fk = getattr(meta, 'include_fk', False)
def __init__(self, meta):
super(TableSchemaOpts, self).__init__(meta)
self.table = getattr(meta, 'table', None)
self.model_converter = getattr(meta, 'model_converter', ModelConverter)
self.include_fk = getattr(meta, 'include_fk', False)