本文整理匯總了Python中flask_sqlalchemy.Model方法的典型用法代碼示例。如果您正苦於以下問題:Python flask_sqlalchemy.Model方法的具體用法?Python flask_sqlalchemy.Model怎麽用?Python flask_sqlalchemy.Model使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類flask_sqlalchemy
的用法示例。
在下文中一共展示了flask_sqlalchemy.Model方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _patch_metadata
# 需要導入模塊: import flask_sqlalchemy [as 別名]
# 或者: from flask_sqlalchemy import Model [as 別名]
def _patch_metadata():
naming_convention = {
'fk': ('fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s'
'_%(referred_column_0_name)s'),
'pk': 'pk_%(table_name)s',
'ix': 'ix_%(table_name)s_%(column_0_name)s',
'ck': 'ck_%(table_name)s_%(constraint_name)s',
'uq': 'uq_%(table_name)s_%(column_0_name)s',
}
metadata = MetaData(naming_convention=naming_convention)
base = declarative_base(cls=flask_sqlalchemy.Model, name='Model',
metaclass=flask_sqlalchemy._BoundDeclarativeMeta,
metadata=metadata)
base.query = flask_sqlalchemy._QueryProperty(db)
db.Model = base
示例2: make_declarative_base
# 需要導入模塊: import flask_sqlalchemy [as 別名]
# 或者: from flask_sqlalchemy import Model [as 別名]
def make_declarative_base(self, model, metadata=None):
if not isinstance(model, DeclarativeMeta):
model = declarative_base(
cls=model, name="Model", metadata=metadata, metaclass=CombinedMeta
)
if metadata is not None and model.metadata is not metadata:
model.metadata = metadata
if not getattr(model, "query_class", None):
model.query_class = self.Query
model.query = _QueryProperty(self)
return model
示例3: is_model
# 需要導入模塊: import flask_sqlalchemy [as 別名]
# 或者: from flask_sqlalchemy import Model [as 別名]
def is_model(name, obj):
is_model_class = inspect.isclass(obj) and issubclass(obj, Model)
base_classes = ('Model',)
return is_model_class and name not in base_classes
示例4: __init__
# 需要導入模塊: import flask_sqlalchemy [as 別名]
# 或者: from flask_sqlalchemy import Model [as 別名]
def __init__(self, *args, **kwargs):
"""
Object initialization:
- set the named attributes and add the object to the database
- create relationships
"""
# All SAFRSBase subclasses have a jsonapi id, passed as "id" in web requests
# if no id is supplied, generate a new safrs id (uuid4)
# instantiate the id with the "id_type", this will validate the id if
# validation is implemented
kwargs["id"] = self.id_type(kwargs.get("id", None))
# Initialize the attribute values: these have been passed as key-value pairs in the
# kwargs dictionary (from json in case of a web request).
# Retrieve the values from each attribute (== class table column)
db_args = {}
for column_name in self._s_column_names:
if column_name in kwargs:
attr_val = self._s_parse_attr_value(column_name, kwargs.get(column_name))
db_args[column_name] = attr_val
# Add the related instances
for rel_name in self._s_relationship_names:
if rel_name in kwargs:
rel_attr = kwargs.get(rel_name)
db_args[rel_name] = rel_attr
# db_args now contains the class attributes. Initialize the DB model with them
# All subclasses should have the DB.Model as superclass.
# (SQLAlchemy doesn't work when using DB.Model as SAFRSBase superclass)
try:
safrs.DB.Model.__init__(self, **db_args)
except Exception as exc:
# OOPS .. things are going bad, this might happen using sqla automap
safrs.log.error("Failed to instantiate {}".format(self))
safrs.log.debug("db args: {}".format(db_args))
safrs.log.exception(exc)
safrs.DB.Model.__init__(self)
if self._s_auto_commit:
# Add the object to the database if specified by the class parameters
safrs.DB.session.add(self)
try:
safrs.DB.session.commit()
except sqlalchemy.exc.SQLAlchemyError as exc:
# Exception may arise when a DB constrained has been violated (e.g. duplicate key)
raise GenericError(exc)