[Answer]-Django: Access raw sql results directly in template without looping – since only returns one row

1👍

results = listings.objects.raw(sql_query, [id,])[0]

or custom method:

def get_results():
   from django.db import connection
   cursor = connection.cursor()
   cursor.execute(sql)
   return cursor.fetchone()
👤cetver

Leave a comment