Search code examples
djangodatagridw2ui

addind list key value to a result of query


i am use django 2.0 and w2ui 1.5 grid i want to show result in the datagrid of w2ui.

The structure of the json for the w2ui datagrid is like

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

my views.py

i use cursor because my query is to long and i don't know how to do it with 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()] ]

result give me this

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

how can i insert {"status": "success"} and the {"total":1} for that the datagrid show it?


Solution

  • You can convert result to dict like this:

    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}