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.
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.
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 :