将列表键值添加到查询结果

addind list key value to a result of query

我正在使用 django 2.0 和 w2ui 1.5 网格我想在 w2ui 的数据网格中显示结果。

w2ui 数据网格的 json 结构类似于

{"status": "success", "total": 1, "records": [{"key": "value", "key2": "value2"} ]}

我的views.py

我使用游标是因为我的查询太长而且我不知道如何用 django 来做

from django.db import connection
cursor = connection.cursor()

query=("select table1.id as id,table1.code as code,table1.nom as nom,table2.nom as tgen\
        from table1, table2\
        where table1.table2_id = table2.id\

        EXCEPT\

       select select table1.id,table1.code,table1.nom,table2.nom\
             from table1,table2\
             where where table1.table2_id = table2.id\
             and table1.id in (select table1_id\
             from table3\
             where table3.id in (select table3_id\
             from table4))")

       cursor.execute(query)

       result = [ dict(line) for line in [zip([ column[0] for column in cursor.description], row) for row in cursor.fetchall()] ]

结果给我这个

[{"id": 1, "nom": "jojo", "typegen": "aloba", "code": "GEN-1"} ]

我如何插入 {"status": "success"} 和数据网格显示的 {"total":1}?

你可以像这样将 result 转换为 dict:

result = [ dict(line) for line in [zip([ column[0] for column in cursor.description], row) for row in cursor.fetchall()] ]
result = {'status': 'success', 'total': len(result), 'records': result}