utils.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:websauna 作者: websauna 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号