C'è un modo per ottenere un cursore dizionario neutro back-end in Django? Questo sarebbe un cursore che è un dict piuttosto che una tupla. Sono costretto a utilizzare Oracle per il progetto scolastico su cui sto lavorando.Django Backend-neutral DictCursor
nel modulo MySQLDb di Python si chiama DictCursor.
con il suggerimento ispiratore di WoLpH so che sono molto vicino ..
def dict_cursor(cursor):
for row in cursor:
yield dict(zip(cursor.description, row))
Iterazione e la stampa di ogni cursore fila utilizzato per provocare:
(482072, 602592, 1)
(656680, 820855, 2)
(574968, 718712, 4)
(557532, 696918, 3))
Ma con dict_cursor ottengo:
{('NET_SPENT', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 482072, ('LOT', <type 'cx_Oracle.NUMBER'>, 12, 22, 11, 0, 0): 1, ('NET_COLLECTED', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 602592}
{('NET_SPENT', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 656680, ('LOT', <type 'cx_Oracle.NUMBER'>, 12, 22, 11, 0, 0): 2, ('NET_COLLECTED', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 820855}
{('NET_SPENT', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 574968, ('LOT', <type 'cx_Oracle.NUMBER'>, 12, 22, 11, 0, 0): 4, ('NET_COLLECTED', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 718712}
{('NET_SPENT', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 557532, ('LOT', <type 'cx_Oracle.NUMBER'>, 12, 22, 11, 0, 0): 3, ('NET_COLLECTED', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 696918}
Voglio solo che utilizzi la chiave, ad es. 'SPESA NETTA'.
dopo la raffinazione un po 'di più, questo sembra funzionare:
def dict_cursor(cursor):
for row in cursor:
out = {}
for i,col in enumerate(cursor.description):
out[col[0]] = row[i]
yield out
-
{'NET_COLLECTED': 602592, 'NET_SPENT': 482072, 'LOT': 1}
{'NET_COLLECTED': 820855, 'NET_SPENT': 656680, 'LOT': 2}
{'NET_COLLECTED': 718712, 'NET_SPENT': 574968, 'LOT': 4}
{'NET_COLLECTED': 696918, 'NET_SPENT': 557532, 'LOT': 3}
Ho modificato il mio cursore, ho frainteso la descrizione dell'oggetto sembra :) – Wolph