Created
December 8, 2009 19:23
-
-
Save codiez/251909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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