Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Module « flask_sqlalchemy »

Classe « SQLAlchemy »

Informations générales

Héritage

builtins.object
    SQLAlchemy

Définition

class SQLAlchemy(builtins.object):

Description [extrait de SQLAlchemy.__doc__]

This class is used to control the SQLAlchemy integration to one
    or more Flask applications.  Depending on how you initialize the
    object it is usable right away or will attach as needed to a
    Flask application.

    There are two usage modes which work very similarly.  One is binding
    the instance to a very specific Flask application::

        app = Flask(__name__)
        db = SQLAlchemy(app)

    The second possibility is to create the object once and configure the
    application later to support it::

        db = SQLAlchemy()

        def create_app():
            app = Flask(__name__)
            db.init_app(app)
            return app

    The difference between the two is that in the first case methods like
    :meth:`create_all` and :meth:`drop_all` will work all the time but in
    the second case a :meth:`flask.Flask.app_context` has to exist.

    By default Flask-SQLAlchemy will apply some backend-specific settings
    to improve your experience with them.

    As of SQLAlchemy 0.6 SQLAlchemy
    will probe the library for native unicode support.  If it detects
    unicode it will let the library handle that, otherwise do that itself.
    Sometimes this detection can fail in which case you might want to set
    ``use_native_unicode`` (or the ``SQLALCHEMY_NATIVE_UNICODE`` configuration
    key) to ``False``.  Note that the configuration key overrides the
    value you pass to the constructor.  Direct support for ``use_native_unicode``
    and SQLALCHEMY_NATIVE_UNICODE are deprecated as of v2.4 and will be removed
    in v3.0.  ``engine_options`` and ``SQLALCHEMY_ENGINE_OPTIONS`` may be used
    instead.

    This class also provides access to all the SQLAlchemy functions and classes
    from the :mod:`sqlalchemy` and :mod:`sqlalchemy.orm` modules.  So you can
    declare models like this::

        class User(db.Model):
            username = db.Column(db.String(80), unique=True)
            pw_hash = db.Column(db.String(80))

    You can still use :mod:`sqlalchemy` and :mod:`sqlalchemy.orm` directly, but
    note that Flask-SQLAlchemy customizations are available only through an
    instance of this :class:`SQLAlchemy` class.  Query classes default to
    :class:`BaseQuery` for `db.Query`, `db.Model.query_class`, and the default
    query_class for `db.relationship` and `db.backref`.  If you use these
    interfaces through :mod:`sqlalchemy` and :mod:`sqlalchemy.orm` directly,
    the default query class will be that of :mod:`sqlalchemy`.

    .. admonition:: Check types carefully

       Don't perform type or `isinstance` checks against `db.Table`, which
       emulates `Table` behavior but is not a class. `db.Table` exposes the
       `Table` interface, but is a function which allows omission of metadata.

    The ``session_options`` parameter, if provided, is a dict of parameters
    to be passed to the session constructor.  See :class:`~sqlalchemy.orm.session.Session`
    for the standard options.

    The ``engine_options`` parameter, if provided, is a dict of parameters
    to be passed to create engine.  See :func:`~sqlalchemy.create_engine`
    for the standard options.  The values given here will be merged with and
    override anything set in the ``'SQLALCHEMY_ENGINE_OPTIONS'`` config
    variable or othewise set by this library.

    .. versionadded:: 0.10
       The `session_options` parameter was added.

    .. versionadded:: 0.16
       `scopefunc` is now accepted on `session_options`. It allows specifying
        a custom function which will define the SQLAlchemy session's scoping.

    .. versionadded:: 2.1
       The `metadata` parameter was added. This allows for setting custom
       naming conventions among other, non-trivial things.

       The `query_class` parameter was added, to allow customisation
       of the query class, in place of the default of :class:`BaseQuery`.

       The `model_class` parameter was added, which allows a custom model
       class to be used in place of :class:`Model`.

    .. versionchanged:: 2.1
       Utilise the same query class across `session`, `Model.query` and `Query`.

    .. versionadded:: 2.4
       The `engine_options` parameter was added.

    .. versionchanged:: 2.4
       The `use_native_unicode` parameter was deprecated.

    .. versionchanged:: 2.4.3
        ``COMMIT_ON_TEARDOWN`` is deprecated and will be removed in
        version 3.1. Call ``db.session.commit()`` directly instead.
    

Constructeur(s)

Signature du constructeur Description
__init__(self, app=None, use_native_unicode=True, session_options=None, metadata=None, query_class=<class 'flask_sqlalchemy.BaseQuery'>, model_class=<class 'flask_sqlalchemy.model.Model'>, engine_options=None)

Liste des attributs statiques

Nom de l'attribut Valeur
QueryNone

Liste des propriétés

Nom de la propriétéDescription
engineGives access to the engine. If the database configuration is bound [extrait de __doc__]
metadataThe metadata associated with ``db.Model``. [extrait de __doc__]

Liste des opérateurs

Opérateurs hérités de la classe object

__eq__, __ge__, __gt__, __le__, __lt__, __ne__

Liste des méthodes

Toutes les méthodes Méthodes d'instance Méthodes statiques Méthodes dépréciées
Signature de la méthodeDescription
__repr__(self)
apply_driver_hacks(self, app, sa_url, options) This method is called before engine creation and used to inject [extrait de apply_driver_hacks.__doc__]
apply_pool_defaults(self, app, options)
create_all(self, bind='__all__', app=None) Creates all tables. [extrait de create_all.__doc__]
create_engine(self, sa_url, engine_opts)
create_scoped_session(self, options=None) Create a :class:`~sqlalchemy.orm.scoping.scoped_session` [extrait de create_scoped_session.__doc__]
create_session(self, options) Create the session factory used by :meth:`create_scoped_session`. [extrait de create_session.__doc__]
drop_all(self, bind='__all__', app=None) Drops all tables. [extrait de drop_all.__doc__]
get_app(self, reference_app=None) Helper method that implements the logic to look up an [extrait de get_app.__doc__]
get_binds(self, app=None) Returns a dictionary with a table->engine mapping. [extrait de get_binds.__doc__]
get_engine(self, app=None, bind=None) Returns a specific engine. [extrait de get_engine.__doc__]
get_tables_for_bind(self, bind=None) Returns a list of all tables relevant for a bind. [extrait de get_tables_for_bind.__doc__]
init_app(self, app) This callback can be used to initialize an application for the [extrait de init_app.__doc__]
make_connector(self, app=None, bind=None) Creates the connector for a given state and bind. [extrait de make_connector.__doc__]
make_declarative_base(self, model, metadata=None) Creates the declarative base that all models will inherit from. [extrait de make_declarative_base.__doc__]
reflect(self, bind='__all__', app=None) Reflects tables from the database. [extrait de reflect.__doc__]

Méthodes héritées de la classe object

__delattr__, __dir__, __format__, __getattribute__, __hash__, __init_subclass__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__