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
    from http://fr.slideshare.net/Counsyl/efficient-djangoquery-setuse

    must be used in the form :

        @track_sql()
        def function_to_track()

        @track_sql(show_queries=True)
        def function_to_track()

    """
    def decorator(func):
        @functools.wraps(func)
        def wraped(*args, **kwargs):
            start = datetime.now()
            query_count = len(con.queries)
            try:
                return func(*args, **kwargs)
            finally:
                print "%s %s" % ("=" * 40, func.__name__)
                print "Time : %s" % (datetime.now() - 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)