Skip to content

Instantly share code, notes, and snippets.

@codiez
Created December 8, 2009 19:23
Show Gist options
  • Save codiez/251909 to your computer and use it in GitHub Desktop.
Save codiez/251909 to your computer and use it in GitHub Desktop.
'''
Runs a RAW SQL query in DJANGO and returns dictionaries to the template rather then a tuple
'''
from myapp.files.models import summary
from django.shortcuts import render_to_response
def generate_dicts(cur):
import itertools
fieldnames = [d[0].lower() for d in cur.description]
while True:
rows = cur.fetchmany()
if not rows: return
for row in rows:
yield dict(itertools.izip(fieldnames, row))
def get_summary(request):
from django.db import connection
cursor = connection.cursor()
sql = '''
select * from files where file_id='xxx'
'''
cursor.execute(sql)
rows = generate_dicts(cursor)
object_list = [row for row in rows]
template = 'summary.html'
return render_to_response(template,{'object_list': object_list, 'MEDIA_URL': '/media/'},)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment