9

I need to find a way to get a list of all of an SQLAlchemy models hybrid properties.

For relationships on a Person instance I can do something like:

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

Is there something like:

 inspect(person.__class__).hybrid_properties
Jakobovski
  • 3,203
  • 1
  • 31
  • 38

1 Answers1

9

Here is a solution I came up with:

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__
Jakobovski
  • 3,203
  • 1
  • 31
  • 38
  • Great great found! Thank you! – Nam G VU Mar 30 '18 at 03:37
  • 1
    Thank you for sharing, it seems the syntax has changed: `[item for item in sa_inspect(Travel).all_orm_descriptors if isinstance(item, hybrid_property)]` does the trick. The `sa_inspect` expects a class definition directly (`__class__` raises `sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type `) – jlandercy Jul 12 '20 at 09:49