cli.py 484 B

12345678910111213141516
  1. from __future__ import annotations
  2. import typing as t
  3. from flask import current_app
  4. def add_models_to_shell() -> dict[str, t.Any]:
  5. """Registered with :meth:`~flask.Flask.shell_context_processor` if
  6. ``add_models_to_shell`` is enabled. Adds the ``db`` instance and all model classes
  7. to ``flask shell``.
  8. """
  9. db = current_app.extensions["sqlalchemy"]
  10. out = {m.class_.__name__: m.class_ for m in db.Model._sa_registry.mappers}
  11. out["db"] = db
  12. return out