from django import template from django db import connection register

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django import template
from django.db import connection
register = template.Library()
@register.inclusion_tag('common/ormdebug.html')
def ormdebug():
import re
queries = connection.queries
query_time = 0
query_count = 0
for query in queries:
query_time += float(query['time'])
query_count += int(1)
query['sql'] = re.sub(r'(FROM|WHERE)', '\n\\1', query['sql'])
query['sql'] = re.sub(r'((?:[^,]+,){3})', '\\1\n ', query['sql'])
return {
'query_time': query_time,
'query_count': query_count,
'queries': queries}