本文整理匯總了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))