Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all of an SQLAlchemy model's hybrid attributes?

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
like image 578
Jakobovski Avatar asked Aug 07 '15 03:08

Jakobovski


People also ask

What is SQLAlchemy hybrid property?

“hybrid” means the attribute has distinct behaviors defined at the class level and at the instance level. The hybrid extension provides a special form of method decorator, is around 50 lines of code and has almost no dependencies on the rest of SQLAlchemy.

What does all () do in SQLAlchemy?

all() method. The Query object, when asked to return full entities, will deduplicate entries based on primary key, meaning if the same primary key value would appear in the results more than once, only one object of that primary key would be present.

What is InstrumentedAttribute?

InstrumentedAttribute is used to define a relationship to another table, for example. Query the database for results, and the resulting objects will have the reference filled in for you: for res in session.query(ResultLine).

What is SQLAlchemy model?

It is an open source and cross-platform software released under MIT license. SQLAlchemy is famous for its object-relational mapper (ORM), using which classes can be mapped to the database, thereby allowing the object model and database schema to develop in a cleanly decoupled way from the beginning.


1 Answers

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__
like image 88
Jakobovski Avatar answered Sep 27 '22 23:09

Jakobovski