From 03e1cc930c6a5fbe993fb120e4403f3dd8a7c211 Mon Sep 17 00:00:00 2001 From: Mounir Messelmeni Date: Sat, 9 Apr 2016 14:09:08 +0200 Subject: [PATCH] Fixed #26145 -- Made debug context processor return queries for all databases. --- django/template/context_processors.py | 9 +++++++-- docs/ref/templates/api.txt | 9 +++++++-- docs/releases/1.10.txt | 3 +++ .../templates/context_processors/debug.html | 4 ++++ tests/context_processors/tests.py | 2 ++ tests/context_processors/views.py | 5 ++++- 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/django/template/context_processors.py b/django/template/context_processors.py index d46cd3ec8f..5568ed9134 100644 --- a/django/template/context_processors.py +++ b/django/template/context_processors.py @@ -9,6 +9,8 @@ of a DjangoTemplates backend and used by RequestContext. from __future__ import unicode_literals +import itertools + from django.conf import settings from django.middleware.csrf import get_token from django.utils.encoding import smart_text @@ -40,10 +42,13 @@ def debug(request): context_extras = {} if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: context_extras['debug'] = True - from django.db import connection + from django.db import connections # Return a lazy reference that computes connection.queries on access, # to ensure it contains queries triggered after this function runs. - context_extras['sql_queries'] = lazy(lambda: connection.queries, list) + context_extras['sql_queries'] = lazy( + lambda: list(itertools.chain(*[connections[x].queries for x in connections])), + list + ) return context_extras diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt index 6edc818730..1bfb1f87a5 100644 --- a/docs/ref/templates/api.txt +++ b/docs/ref/templates/api.txt @@ -699,8 +699,13 @@ the request's IP address (``request.META['REMOTE_ADDR']``) is in the you're in :setting:`DEBUG` mode. * ``sql_queries`` -- A list of ``{'sql': ..., 'time': ...}`` dictionaries, representing every SQL query that has happened so far during the request - and how long it took. The list is in order by query and lazily generated - on access. + and how long it took. The list is in order by database alias and then by + query. It's lazily generated on access. + +.. versionchanged:: 1.10 + + In older versions, only the queries for the default database alias were + included. ``django.template.context_processors.i18n`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 490aa30fb7..adae2524da 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -414,6 +414,9 @@ Templates * Allowed :tfilter:`dictsort` to order a list of lists by an element at a specified index. +* The :func:`~django.template.context_processors.debug` context processor + contains queries for all database aliases instead of only the default alias. + Tests ~~~~~ diff --git a/tests/context_processors/templates/context_processors/debug.html b/tests/context_processors/templates/context_processors/debug.html index 671ab81372..076fa4cd87 100644 --- a/tests/context_processors/templates/context_processors/debug.html +++ b/tests/context_processors/templates/context_processors/debug.html @@ -12,4 +12,8 @@ Second query list: {{ sql_queries|length }} Third query list: {{ sql_queries|length }} +{% for obj in other_debug_objects.all %}{{ obj }}{% endfor %} + +Fourth query list: {{ sql_queries|length }} + {% endif %} diff --git a/tests/context_processors/tests.py b/tests/context_processors/tests.py index de25735b5c..f9c461cad9 100644 --- a/tests/context_processors/tests.py +++ b/tests/context_processors/tests.py @@ -87,3 +87,5 @@ class DebugContextProcessorTests(TestCase): self.assertContains(response, 'Second query list: 1') # Check we have not actually memoized connection.queries self.assertContains(response, 'Third query list: 2') + # Check queries for DB connection 'other' + self.assertContains(response, 'Fourth query list: 3') diff --git a/tests/context_processors/views.py b/tests/context_processors/views.py index 1a7eda0b16..354e842a8d 100644 --- a/tests/context_processors/views.py +++ b/tests/context_processors/views.py @@ -8,5 +8,8 @@ def request_processor(request): def debug_processor(request): - context = {'debug_objects': DebugObject.objects} + context = { + 'debug_objects': DebugObject.objects, + 'other_debug_objects': DebugObject.objects.using('other'), + } return render(request, 'context_processors/debug.html', context)