當前位置: 首頁>>代碼示例>>Python>>正文


Python flask_sqlalchemy.Model方法代碼示例

本文整理匯總了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 
開發者ID:paxswill,項目名稱:evesrp,代碼行數:17,代碼來源:__init__.py

示例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 
開發者ID:hjlarry,項目名稱:flask-shop,代碼行數:15,代碼來源:extensions.py

示例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 
開發者ID:briancappello,項目名稱:flask-react-spa,代碼行數:6,代碼來源:magic.py

示例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) 
開發者ID:thomaxxl,項目名稱:safrs,代碼行數:49,代碼來源:base.py


注:本文中的flask_sqlalchemy.Model方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。