python - Using dict_cursor in django -
to cursor in django do:
from django.db import connection cursor = connection.cursor() how dict cursor in django, equivalent of -
import mysqldb connection = (establish connection) dict_cursor = connection.cursor(mysqldb.cursors.dictcursor) is there way in django? when tried cursor = connection.cursor(mysqldb.cursors.dictcursor) got exception value: cursor() takes 1 argument (2 given). or need connect directly python-mysql driver?
the django docs suggest using dictfetchall:
def dictfetchall(cursor): "returns rows cursor dict" desc = cursor.description return [ dict(zip([col[0] col in desc], row)) row in cursor.fetchall() ] is there performance difference between using , creating dict_cursor?
no there no such support dictcursor in django. can write small function you, see ticket:
def dictfetchall(cursor): "returns rows cursor dict" desc = cursor.description return [ dict(zip([col[0] col in desc], row)) row in cursor.fetchall() ] >>> cursor.execute("select id, parent_id test limit 2"); >>> dictfetchall(cursor) [{'parent_id': none, 'id': 54360982l}, {'parent_id': none, 'id': 54360880l}]
Comments
Post a Comment