def attach_models_to_base_from_module(mod:ModuleType, Base:type):
"""Attach all models in a Python module to SQLAlchemy base class.
The attachable models must declare ``__tablename__`` property and must not have existing ``Base`` class in their inheritance.
"""
for key in dir(mod):
value = getattr(mod, key)
if inspect.isclass(value):
# TODO: We can't really check for SQLAlchemy declarative base class as it's usually run-time generated and may be out of our control
if any(base.__name__ == "Base" for base in inspect.getmro(value)):
# Already inhertis from SQALchemy declarative Base
continue
if hasattr(value, "__tablename__"):
# This declares table but is not attached to any base yet
attach_model_to_base(value, Base)
评论列表
文章目录