I'm developing my first Flask app using SQLAlchemy models, I've got around 10 classes representing database tables and the classes have fields representing columns.
Now in my app.py
I'm trying to make a selection of data from the database and get it to have a form of a list of dictionaries (one dictionary representing a row):
[{'column_name1': 'data1', 'column_name2': 'data2', ...}, {'column_name1': 'data3', 'column_name2': 'data4', ...}, ...]
.
This is what I came up with but it doesn't work:
column_names = table.__table__.columns.keys() # get columns names
records = table.query.whooshee_search(phrase).all() # get table records
DATA = []
for record in records:
row = {}
for column_name in column_names:
row[column_name] = record[column_name]
DATA.append(row)
table
is earlier assigned one of the classes, then column_names
is a list of strings which are the names of the columns defined in this class. records
is a list of table
class objects which represent selected rows (that contain phrase
). I checked displaying the results in html
that this works fine and returns what is suppposed to return.
However, I'd like to have them as a dictionary created dynamically (so that I don't have to specify column names for every possible table
value but make use of column_names
). The line row[column_name] = record[column_name]
seems to be the problem - I got the error
TypeError: 'User' object is not subscriptable
where User
is one of the classes (the current value of table
). row[column_name] = record.column_name
was my another (silly) approach which of course didn't work either. One last thing to mention is that {{ record[column_name] }}
was Jinja2
syntax I used when displaying the results in html
.