1

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.

2
  • Hmm. Are you asking if it can be determined programmatically, at runtime, or manually (by debugging and inspecting models objects, logging, etc..)? Commented Nov 20, 2013 at 11:44
  • Sorry for ambiguity - question updated. Commented Nov 20, 2013 at 12:17

1 Answer 1

1

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)
    )
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.