convert sqlalchemy query result to a list of dicts
python, sqlalchemy
Solution
Try
result_dict = [u.__dict__ for u in my_query.all()]
Besides what is the type of your `result_dict` before the `for` loop? Its behavior is rather strange.
Problem
I want to have the result of my query converted to a list of dicts like this : ``` result_dict = [{'category': 'failure', 'week': '1209', 'stat': 'tdc_ok', 'severityDue': '2_critic'}, {'category': 'failure', 'week': '1210', 'stat': 'tdc_nok', 'severityDue': '2_critic'}] ``` But instead I get it as a dict, thus with repeated keys: ``` result_dict = {'category': 'failure', 'week': '1209', 'stat': 'tdc_ok', 'severityDue': '2_critic', 'category': 'failure', 'week': '1210', 'stat': 'tdc_nok', 'severityDue': '2_critic'} ``` I get this result by doing this : ``` for u in my_query.all(): result_dict = u.__dict__ ``` How can I convert sqlAlchemy query result to a list of dicts (each row would be a dict) ? Help please