如何找到所有SQLAlchemy模型的混合属性?

时间:2015-08-07 03:49:00

标签: python sqlalchemy

我需要找到一种方法来获取所有SQLAlchemy模型混合属性的列表。

对于Person实例上的关系,我可以执行以下操作:

CREATE OR REPLACE FUNCTION f_update_labels() RETURNS TRIGGER AS $update_labels$
BEGIN
  --set FilingCabinet label to be floor:room
  NEW.label := to_char(NEW.floor, 'FM00') || ':' || to_char(NEW.room, 'FM00');

  --set Drawer label to be floor:room:drawerRow:drawerCol
  UPDATE drawer
  SET label = NEW.label || ':' || to_char(drawerrow, 'FM00') || ':' || to_char(drawercol, 'FM00')
  WHERE id = NEW.drawerid;

  RETURN NEW;
END; $update_labels$ LANGUAGE plpgsql;

是否有类似的东西:

from sqlalchemy.inspection import inspect
inspect(person.__class__).relationships 

1 个答案:

答案 0 :(得分:4)

这是我提出的解决方案:

from sqlalchemy.inspection import inspect as sa_inspect
from sqlalchemy.ext.hybrid import hybrid_property

for item in sa_inspect(A_MODEL_INSTANCE.__class__).all_orm_descriptors:
    if type(item) == hybrid_property:
        print item.__name__