문제

Let's say that I have SQLAlchemy ORM model instance model and name of it's attribute attr_name.

Is there a way to determine whether getattr(model, attr_name) will or won't query the database? I mean that I need a function getattr_does_emit_sql(model, attr_name) returning True or False.

도움이 되었습니까?

해결책

Solved it. The function is

from sqlalchemy.orm.attributes import QueryableAttribute
def getattr_does_emit_sql(model, attr_name):
    return (attr_name not in model.__dict__ 
        and hasattr(type(model), attr_name
        and isinstance(getattr(type(model), attr_name), QueryableAttribute)
    )
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top