Ticket #12763: django_db_debug_query_caller.diff

File django_db_debug_query_caller.diff, 1.2 KB (added by Daniel Marohn, 15 years ago)

a simple patch

  • django/db/backends/util.py

     
    88except ImportError:
    99    from django.utils import _decimal as decimal    # for Python 2.3
    1010
     11import traceback
     12import re
     13
     14def _query_caller(dbpath_re = re.compile('.*/django/db/.*')):
     15    """Returns a formatted traceback containing only the ORM invocation that initiated this query."""
     16    return traceback.format_list([t for t in traceback.extract_stack() if not dbpath_re.match(t[0])][-1:])
     17
    1118class CursorDebugWrapper(object):
    1219    def __init__(self, cursor, db):
    1320        self.cursor = cursor
     
    2330            self.db.queries.append({
    2431                'sql': sql,
    2532                'time': "%.3f" % (stop - start),
     33                'caller': _query_caller(),
    2634            })
    2735
    2836    def executemany(self, sql, param_list):
     
    3442            self.db.queries.append({
    3543                'sql': '%s times: %s' % (len(param_list), sql),
    3644                'time': "%.3f" % (stop - start),
     45                'caller': _query_caller(),
    3746            })
    3847
    3948    def __getattr__(self, attr):
Back to Top