Because sometimes the django debug toolbar is just overkill, I wrote this decorator.

Code highly stolen from these excelents slides by @jaysonfalkner.

See comments in code for usage.

# coding: utf-8
from datetime import datetime
from django.db import connection as con
import functools

def track_sql(show_queries=False):
    """ track_sql decorator

    must be used in the form :

        def function_to_track()

        def function_to_track()

    def decorator(func):
        def wraped(*args, **kwargs):
            start =
            query_count = len(con.queries)
                return func(*args, **kwargs)
                print "%s %s" % ("=" * 40, func.__name__)
                print "Time : %s" % ( - start)
                print "Queries : %s" % (len(con.queries) - query_count)
                if show_queries:
                    for query in con.queries[query_count:]:
                        print "-" * 10
                        print query['sql']
                print "%s /%s" % ("=" * 40, func.__name__)
        return wraped
    return decorator

(And yes, it would be better if it was also context manager)