+
+
+ {% if query.origin %}
+ {% for origin in query.origin %}
+
+
{{ origin.1 }}({{ origin.2 }})
+
{{ origin.6 }}()
+
{{ origin.4.0 }}
+
+ {% endfor %}
+ {% endif %}
+
+
+
+
+
+
+
+
{{ query.time }}
{{ query.time_accum }}
diff --git a/patch/README b/patch/README
new file mode 100644
index 000000000..1b355b6db
--- /dev/null
+++ b/patch/README
@@ -0,0 +1,10 @@
+
+This directory is for patches to the datatracker environment, not for patches
+to the datatracker itself (those are ephemeral, in the sense that they should
+not stay around as patches, they should be committed to the repository if they
+are of long-term value).
+
+As an example, trace-django-queryset-origin.patch is a patch to django 1.10
+which adds some meta-information to the query entries provided for debug
+purpose by django.template.context_processors.debug as sql_queries.
+
diff --git a/patch/trace-django-queryset-origin.patch b/patch/trace-django-queryset-origin.patch
new file mode 100644
index 000000000..d23eb8357
--- /dev/null
+++ b/patch/trace-django-queryset-origin.patch
@@ -0,0 +1,196 @@
+--- ../x/env/lib/python2.7/site-packages/django/db/models/query.py 2017-04-07 15:10:29.426831000 -0700
++++ env/lib/python2.7/site-packages/django/db/models/query.py 2017-04-07 15:18:39.938521412 -0700
+@@ -5,6 +5,8 @@
+ import copy
+ import sys
+ import warnings
++import inspect
++import logging
+ from collections import OrderedDict, deque
+
+ from django.conf import settings
+@@ -34,6 +36,7 @@
+ # Pull into this namespace for backwards compatibility.
+ EmptyResultSet = sql.EmptyResultSet
+
++logger = logging.getLogger('django.db.backends')
+
+ class BaseIterable(object):
+ def __init__(self, queryset):
+@@ -51,6 +54,7 @@
+ compiler = queryset.query.get_compiler(using=db)
+ # Execute the query. This will also fill compiler.select, klass_info,
+ # and annotations.
++
+ results = compiler.execute_sql()
+ select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
+ compiler.annotation_col_map)
+@@ -174,6 +178,8 @@
+ self._known_related_objects = {} # {rel_field, {pk: rel_obj}}
+ self._iterable_class = ModelIterable
+ self._fields = None
++ self._origin = []
++ self._djangodir = __file__[:(__file__.index('django')+len('django')+1)]
+
+ def as_manager(cls):
+ # Address the circular dependency between `Queryset` and `Manager`.
+@@ -316,6 +322,37 @@
+ combined.query.combine(other.query, sql.OR)
+ return combined
+
++ def _add_origin(self, depth=1):
++ if settings.DEBUG:
++ # get list of frame records. Each is:
++ # [ frame, filename, lineno, function, code_context, index ]
++ stack = inspect.stack()
++ # caller stack record
++ method = stack[depth][3]
++ # look for the first stack entry which is not from django
++ i = 0
++ while i