-
-
Save fierroformo/149f7b9b31a5a9b91e7bf421a581e0ce to your computer and use it in GitHub Desktop.
django decorator for logging db queries
This file contains hidden or 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
# simple decorator for logging out django db queries | |
# just put it along with other decorators before view or | |
# db-loading stuff. than go to your console and see whats going on. | |
# example: | |
# | |
# @log_db_queries | |
# @login_required | |
# def your_view(request, args...): | |
# your stuff | |
def log_db_queries(f): | |
from django.db import connection | |
def new_f(*args, **kwargs): | |
res = f(*args, **kwargs) | |
print "-"*80 | |
print "db queries log for %s:\n" % (f.func_name) | |
print "TOTAL COUNT: %s" % len(connection.queries) | |
print "TOTAL TIME: %s\n" % reduce(lambda x,y: x + float(y["time"]), connection.queries, 0.0) | |
for q in connection.queries: | |
print "%s: %s\n" % (q["time"], q["sql"]) | |
print "-"*80 | |
return res | |
return new_f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment