[Django] #23364: sql_queries from debug template context processor always empty

6 views
Skip to first unread message

Django

unread,
Aug 26, 2014, 3:24:57 PM8/26/14
to django-...@googlegroups.com
#23364: sql_queries from debug template context processor always empty
-------------------------------+--------------------
Reporter: Markush2010 | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
The `sql_queries` template context variable should propagated with all the
queries from `connection.queries`. However, this list seems to be always
empty:

{{{#!python
# django.core.context_processors
def debug(request):
"Returns context variables helpful for debugging."
context_extras = {}
import ipdb; ipdb.set_trace()
if settings.DEBUG and request.META.get('REMOTE_ADDR') in
settings.INTERNAL_IPS:
context_extras['debug'] = True
from django.db import connection
context_extras['sql_queries'] = connection.queries
return context_extras
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23364>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 26, 2014, 3:49:38 PM8/26/14
to django-...@googlegroups.com
#23364: sql_queries from debug template context processor always empty
-------------------------------+------------------------------------

Reporter: Markush2010 | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by aaugustin):

* needs_better_patch: => 0
* needs_docs: => 0
* component: Uncategorized => HTTP handling
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

This is interesting.

Here's the relevant template from the sample project. `mymodels` is a
queryset and it isn't evaluated yet. `sql_queries` is set to
`connection.queries` in the `debug` context processor.

{{{
{% for mymodel in mymodels %}
<li>{{ mymodel.foo }}</li>
{% endfor %}
<pre>
{{ sql_queries }}
{{ sql_queries|length }}
</pre>
}}}

In Django 1.7, `connection.queries` is a list that gets updated with each
new database query. If you print the list in the context processor, it's
empty. But by the time you reach `{{ sql_queries }}` in the template, `{%
for mymodel in mymodels %}` has triggered a query, and `{{ sql_queries }}`
contains that query.

In Django pre-1.8, `connection.queries` is a snapshot of the ring buffer
that prevents unlimited memory consumption in long running processes. Once
the snapshot is made, it isn't updated with new database queries. As a
consequence, it's still empty when reaching `{{ sql_queries }}` in the
template.

I'm not sure what to do. Since it's common to have database queries
triggered by templates in Django projects, this unintentional change of
behavior will be considered a regression by most users. But having `{{
sql_queries }}` return different values depending on where you put it in
the template isn't a good API either.

--
Ticket URL: <https://code.djangoproject.com/ticket/23364#comment:1>

Django

unread,
Aug 26, 2014, 4:56:27 PM8/26/14
to django-...@googlegroups.com
#23364: sql_queries from debug template context processor always empty
-------------------------------+------------------------------------

Reporter: Markush2010 | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Markush2010):

* cc: Markush2010 (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/23364#comment:2>

Django

unread,
Aug 27, 2014, 5:20:22 AM8/27/14
to django-...@googlegroups.com
#23364: sql_queries from debug template context processor always empty
-------------------------------+------------------------------------

Reporter: Markush2010 | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by bpeschier):

* has_patch: 0 => 1


Comment:

Made sql_queries lazy and added tests for the debug context processor:
https://github.com/bpeschier/django/compare/ticket_23364

--
Ticket URL: <https://code.djangoproject.com/ticket/23364#comment:3>

Django

unread,
Aug 27, 2014, 5:42:20 AM8/27/14
to django-...@googlegroups.com
#23364: sql_queries from debug template context processor always empty
-------------------------------+-------------------------------------
Reporter: Markush2010 | Owner: aaugustin
Type: Bug | Status: assigned

Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+-------------------------------------
Changes (by aaugustin):

* owner: nobody => aaugustin
* status: new => assigned


Comment:

Looks good! I'll take it from there.

--
Ticket URL: <https://code.djangoproject.com/ticket/23364#comment:4>

Django

unread,
Aug 30, 2014, 7:29:32 AM8/30/14
to django-...@googlegroups.com
#23364: sql_queries from debug template context processor always empty
-------------------------------+-------------------------------------
Reporter: Markush2010 | Owner: aaugustin
Type: Bug | Status: closed

Component: HTTP handling | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+-------------------------------------
Changes (by Aymeric Augustin <aymeric.augustin@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"eacf244506d8b7e6dd6483834ea122fec864da85"]:
{{{
#!CommitTicketReference repository=""
revision="eacf244506d8b7e6dd6483834ea122fec864da85"
Converted sql_queries into a lazily evaluated list.

Fixed #23364. Thanks Markush2010 for the report.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23364#comment:5>

Reply all
Reply to author
Forward
0 new messages