1# I needed a way to query by referring to the model's tablename. This is what I came up with:
2
3class Model1(db.Model):
4 __tablename__ = 'table1' # or whatever, doesn't matter
5
6class Model2(db.Model):
7 __tablename__ = 'table2'
8
9def table_object(table_name):
10 tables_dict = {table.__tablename__: table for table in db.Model.__subclasses__()}
11 return tables_dict.get(table_name)
12
13# Then, use it like this:
14
15model_list = ['table1', 'table2']
16for model in model_list:
17 some_var = db.session.query(table_object(table_name=model)).filter_by(name='something').all()
18
19# The important bit is db.Model.__subclasses__() - gives a list of the model classes (objects?).