def query_to_dicts(query_string, *query_args):
    """Run a simple query and produce a generator
    that returns the results as a bunch of dictionaries
    with keys for the column values selected.
    """
cursor = connection.cursor()
cursor.execute(query_string, query_args)
col_names = [desc[0] for desc in cursor.description]
while True:
    row = cursor.fetchone()
if row is None:
    break
row_dict = dict(zip(col_names, row))
yield row_dict
return
How to get field names when running plain sql query in django