diff --git a/debug_toolbar/panels/profiling.py b/debug_toolbar/panels/profiling.py index 4613a3cad..8ae1db0e9 100644 --- a/debug_toolbar/panels/profiling.py +++ b/debug_toolbar/panels/profiling.py @@ -1,5 +1,6 @@ import cProfile import os +import tempfile from colorsys import hsv_to_rgb from pstats import Stats @@ -168,8 +169,13 @@ def generate_stats(self, request, response): self.stats = Stats(self.profiler) self.stats.calc_callees() - root_func = cProfile.label(super().process_request.__code__) + prof_file_path = os.path.join( + tempfile.gettempdir(), next(tempfile._get_candidate_names()) + ".prof" + ) + self.profiler.dump_stats(prof_file_path) + self.prof_file_path = prof_file_path + root_func = cProfile.label(super().process_request.__code__) if root_func in self.stats.stats: root = FunctionCall(self.stats, root_func, depth=0) func_list = [] @@ -182,4 +188,6 @@ def generate_stats(self, request, response): dt_settings.get_config()["PROFILER_MAX_DEPTH"], cum_time_threshold, ) - self.record_stats({"func_list": func_list}) + self.record_stats( + {"func_list": func_list, "prof_file_path": self.prof_file_path} + ) diff --git a/debug_toolbar/panels/sql/tracking.py b/debug_toolbar/panels/sql/tracking.py index 477106fdd..c2aff7609 100644 --- a/debug_toolbar/panels/sql/tracking.py +++ b/debug_toolbar/panels/sql/tracking.py @@ -142,7 +142,20 @@ def _last_executed_query(self, sql, params): # process during the .last_executed_query() call. self.db._djdt_logger = None try: - return self.db.ops.last_executed_query(self.cursor, sql, params) + # Handle executemany: take the first set of parameters for formatting + if ( + isinstance(params, (list, tuple)) + and len(params) > 0 + and isinstance(params[0], (list, tuple)) + ): + sample_params = params[0] + else: + sample_params = params + + try: + return self.db.ops.last_executed_query(self.cursor, sql, sample_params) + except Exception: + return sql finally: self.db._djdt_logger = self.logger diff --git a/debug_toolbar/templates/debug_toolbar/panels/profiling.html b/debug_toolbar/templates/debug_toolbar/panels/profiling.html index 0c2206a13..39e8eeb93 100644 --- a/debug_toolbar/templates/debug_toolbar/panels/profiling.html +++ b/debug_toolbar/templates/debug_toolbar/panels/profiling.html @@ -1,4 +1,13 @@ {% load i18n %} + +{% if prof_file_path %} +
+{% endif %} +