在SQLAlchemy类中查找所有关联代理 [英] Finding all association proxies in a SQLAlchemy class

查看:68
本文介绍了在SQLAlchemy类中查找所有关联代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一种从类A(继承自db.Model的类)中获取所有适当的SQLA列的简便方法-我可以在运行时执行 A .__ table __.columns .但是,A还具有多个关联代理,而且我不知道一种优雅的方式来获取这些关联的列表(除了仅在类的所有属性上运行之外).也许可以使用SQLA的检查方法吗?

I know an easy way to get all proper SQLA columns from a class A (that inherits from db.Model) - I can do A.__table__.columns at runtime. However, A also has several association proxies, and I don't know of an elegant way to get a list of those (other than just running on all of the class' attributes). Is there maybe a way to use SQLA's inspect for this?

我正在使用python 2.7和Flask-SQLAlchemy.

I'm using python 2.7 and Flask-SQLAlchemy.

推荐答案

有一种使用

There is a way using the runtime inspection API. With it you wouldn't even need to poke at the internals of A through __table__:

from sqlalchemy import inspect

inspect(A).columns

例如,有关关联代理的列表,您可以这样做

For a list of association proxies you could for example do

from sqlalchemy.ext.associationproxy import ASSOCIATION_PROXY

proxies = [desc for desc in inspect(A).all_orm_descriptors
           if desc.extension_type is ASSOCIATION_PROXY]

<代码> all_orm_descriptors InspectionAttr 属性的集合,其中包括映射的属性以及扩展声明的属性.要区分扩展名和非扩展名,请检查

all_orm_descriptors is a collection of InspectionAttr attributes, which includes mapped attributes as well as attributes declared by extensions. To differentiate between extensions, and non-extensions, inspect the InspectionAttr.extension_type constant attribute.

这篇关于在SQLAlchemy类中查找所有关联代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆