本文整理汇总了Python中marshmallow_sqlalchemy.ModelSchema方法的典型用法代码示例。如果您正苦于以下问题:Python marshmallow_sqlalchemy.ModelSchema方法的具体用法?Python marshmallow_sqlalchemy.ModelSchema怎么用?Python marshmallow_sqlalchemy.ModelSchema使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类marshmallow_sqlalchemy
的用法示例。
在下文中一共展示了marshmallow_sqlalchemy.ModelSchema方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import marshmallow_sqlalchemy [as 别名]
# 或者: from marshmallow_sqlalchemy import ModelSchema [as 别名]
def __init__(self, cls):
super(ModelResourceOptions, self).__init__(cls)
self.name = (self.meta and getattr(self.meta, 'name', None)) or \
self.model and self.model.__tablename__ or self.name
if not self.model:
return None
if not cls.Schema:
meta = type('Meta', (object,), dict({'model': self.model}, **self.schema_meta))
cls.Schema = type(
self.name.title() + 'Schema', (ModelSchema,), dict({'Meta': meta}, **self.schema))
if not self.primary_key:
col = inspect(self.model).primary_key[0]
self.primary_key = col.name
# Flask-SQLAlchemy support
if not self.session and hasattr(self.model, 'query'):
self.session = self.model.query.session
示例2: __init__
# 需要导入模块: import marshmallow_sqlalchemy [as 别名]
# 或者: from marshmallow_sqlalchemy import ModelSchema [as 别名]
def __init__(self, *args, **kwargs):
"""
Combine the inits for marshmallow_jsonapi.Schema, marshmallow_sqlalchemy.ModelSchema.
Forces session=db.connect().
"""
# control if unwrap_item checks for the id field in the object.
self.load_existing = True
# Each instance of the schema should have the current session with the DB
# (marshmallow-sqlschema). This must be done on instance init, not on class creation!
kwargs['session'] = db.connect()
super().__init__(*args, **kwargs)
示例3: __init__
# 需要导入模块: import marshmallow_sqlalchemy [as 别名]
# 或者: from marshmallow_sqlalchemy import ModelSchema [as 别名]
def __init__(self, *args, **kwargs):
session = kwargs.pop('session', None)
self.instance = kwargs.pop('instance', None)
super(ModelSchema, self).__init__(*args, **kwargs)
self.session = session or self.opts.sqla_session
示例4: load
# 需要导入模块: import marshmallow_sqlalchemy [as 别名]
# 或者: from marshmallow_sqlalchemy import ModelSchema [as 别名]
def load(self, data, session=None, instance=None, *args, **kwargs):
"""Deserialize data to internal representation.
:param session: Optional SQLAlchemy session.
:param instance: Optional existing instance to modify.
"""
self.session = session or self.session
self.instance = instance or self.instance
if not self.session:
raise ValueError('Deserialization requires a session')
return super(ModelSchema, self).load(data, *args, **kwargs)
示例5: validate
# 需要导入模块: import marshmallow_sqlalchemy [as 别名]
# 或者: from marshmallow_sqlalchemy import ModelSchema [as 别名]
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)
示例6: load
# 需要导入模块: import marshmallow_sqlalchemy [as 别名]
# 或者: from marshmallow_sqlalchemy import ModelSchema [as 别名]
def load(self, data, session=None, instance=None, *args, **kwargs):
"""Deserialize data to internal representation.
:param session: Optional SQLAlchemy session.
:param instance: Optional existing instance to modify.
"""
self.session = session or self.session
self.instance = instance or self.instance
if not self.session:
raise ValueError('Deserialization requires a session')
ret = super(ModelSchema, self).load(data, *args, **kwargs)
self.instance = None
return ret
示例7: register_schemas
# 需要导入模块: import marshmallow_sqlalchemy [as 别名]
# 或者: from marshmallow_sqlalchemy import ModelSchema [as 别名]
def register_schemas(Base):
"""Sets the __marshmallow__ attribute on all model classes.
Model classes are all the models under the provided declarative Base.
The __marshmallow__ attribute specifies which schema to use for general serialization of this model
(esp. in the API).
Such a schema can be explicitely defined in this module (needs to inherit from ModelSchema and
have a class name ending in "Schema"). If not, it is inferred from the model.
"""
_register_explicit_schemas()
_register_deduced_schemas(Base)()
# If the mapper configuration is not complete yet, models will not be stored
# in the declarative Base, hence this trigger:
sa.event.listen(orm.mapper, 'after_configured', _register_deduced_schemas(Base))