B
B
beduin012018-03-21 18:00:44
Python
beduin01, 2018-03-21 18:00:44

Is it possible to dynamically substitute the table name?

I have a dictionary like key : value. The key contains the name of the column, value contains its value. Is it possible somehow in the dynamics to substitute the values ​​of the keys as identifiers of the names of the columns.

query = session.query(Transaction)
for k, v in incl.items():
  if isinstance(v, list):
    query = query.filter(Transaction.id.in_(v))

It means to get a request like:
query = query.filter(Transaction.key1.in_(v))
query = query.filter(Transaction.key2.in_(v))
query = query.filter(Transaction.key3.in_(v))

`Transaction` respectively model name

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-03-21
@beduin01

query = session.query(Transaction)
for k, v in incl.items():
  column = getattr(Transaction, k)
  if isinstance(v, list):
    query = query.filter(column.in_(v))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question