0

I am using sqlalchemy and i wonder if its possible to dynamically modify an attribute of a class while doing a query

if school == "abc":
         school_data = (
             meta.session.query(
                 model.School.name, <=== HERE
...

i want to be able to dynamically change .name. to another field, is it possible with python and sqlalchemy?

2 Answers 2

2

You can try to use getattr :

if school == "abc":
   attr = "name"
else:
   attr = "id"

school_data = meta.session.query(getattr(model.School, attr) ...)

Sign up to request clarification or add additional context in comments.

Comments

0

I am not 100% sure if this is what you need, but you can do

c = "name"
getattr(table.c, c)

this gets a column from a table, named c - which can be any string. I am quite sure you can adapt this to model.School

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.