Skip to content

Commit e0aed58

Browse files
committed
Updated a django patch to add origin information to query sets.
- Legacy-Id: 14792
1 parent 49f00b7 commit e0aed58

1 file changed

Lines changed: 24 additions & 40 deletions

File tree

patch/trace-django-queryset-origin.patch

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
--- django/db/models/query.py 2017-04-07 15:10:29.426831000 -0700
2-
+++ django/db/models/query.py 2017-04-12 08:23:20.061651277 -0700
1+
--- /home/henrik/src/db/latest/env/lib/python2.7/site-packages/django/db/models/query.py 2018-03-13 07:07:54.156249000 -0700
2+
+++ /home/henrik/src/db/trunk/env/lib/python2.7/site-packages/django/db/models/query.py 2018-03-14 09:06:43.378819023 -0700
33
@@ -5,6 +5,7 @@
44
import copy
55
import sys
@@ -8,32 +8,16 @@
88
from collections import OrderedDict, deque
99

1010
from django.conf import settings
11-
@@ -34,7 +35,6 @@
12-
# Pull into this namespace for backwards compatibility.
13-
EmptyResultSet = sql.EmptyResultSet
14-
15-
-
16-
class BaseIterable(object):
17-
def __init__(self, queryset):
18-
self.queryset = queryset
19-
@@ -51,6 +51,7 @@
20-
compiler = queryset.query.get_compiler(using=db)
21-
# Execute the query. This will also fill compiler.select, klass_info,
22-
# and annotations.
23-
+
24-
results = compiler.execute_sql()
25-
select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
26-
compiler.annotation_col_map)
27-
@@ -174,6 +175,8 @@
28-
self._known_related_objects = {} # {rel_field, {pk: rel_obj}}
11+
@@ -171,6 +172,8 @@
12+
self._known_related_objects = {} # {rel_field: {pk: rel_obj}}
2913
self._iterable_class = ModelIterable
3014
self._fields = None
3115
+ self._origin = []
3216
+ self._djangodir = __file__[:(__file__.index('django')+len('django')+1)]
3317

3418
def as_manager(cls):
3519
# Address the circular dependency between `Queryset` and `Manager`.
36-
@@ -316,6 +319,31 @@
20+
@@ -310,6 +313,31 @@
3721
combined.query.combine(other.query, sql.OR)
3822
return combined
3923

@@ -65,122 +49,122 @@
6549
####################################
6650
# METHODS THAT DO DATABASE QUERIES #
6751
####################################
68-
@@ -793,6 +821,7 @@
52+
@@ -781,6 +809,7 @@
6953
Returns a new QuerySet instance with the args ANDed to the existing
7054
set.
7155
"""
7256
+ self._add_origin()
7357
return self._filter_or_exclude(False, *args, **kwargs)
7458

7559
def exclude(self, *args, **kwargs):
76-
@@ -800,6 +829,7 @@
60+
@@ -788,6 +817,7 @@
7761
Returns a new QuerySet instance with NOT (args) ANDed to the existing
7862
set.
7963
"""
8064
+ self._add_origin()
8165
return self._filter_or_exclude(True, *args, **kwargs)
8266

8367
def _filter_or_exclude(self, negate, *args, **kwargs):
84-
@@ -824,6 +854,7 @@
68+
@@ -812,6 +842,7 @@
8569
This exists to support framework features such as 'limit_choices_to',
8670
and usually it will be more natural to use other methods.
8771
"""
8872
+ self._add_origin()
8973
if isinstance(filter_obj, Q) or hasattr(filter_obj, 'add_to_query'):
9074
clone = self._clone()
9175
clone.query.add_q(filter_obj)
92-
@@ -836,6 +867,7 @@
93-
Returns a new QuerySet instance that will select objects with a
94-
FOR UPDATE lock.
76+
@@ -866,6 +897,7 @@
9577
"""
78+
if nowait and skip_locked:
79+
raise ValueError('The nowait option cannot be used with skip_locked.')
9680
+ self._add_origin()
9781
obj = self._clone()
9882
obj._for_write = True
9983
obj.query.select_for_update = True
100-
@@ -855,6 +887,7 @@
84+
@@ -886,6 +918,7 @@
10185
if self._fields is not None:
10286
raise TypeError("Cannot call select_related() after .values() or .values_list()")
10387

10488
+ self._add_origin()
10589
obj = self._clone()
10690
if fields == (None,):
10791
obj.query.select_related = False
108-
@@ -874,6 +907,7 @@
92+
@@ -905,6 +938,7 @@
10993
prefetch is appended to. If prefetch_related(None) is called, the list
11094
is cleared.
11195
"""
11296
+ self._add_origin()
11397
clone = self._clone()
11498
if lookups == (None,):
115-
clone._prefetch_related_lookups = []
116-
@@ -886,6 +920,7 @@
99+
clone._prefetch_related_lookups = ()
100+
@@ -917,6 +951,7 @@
117101
Return a query set in which the returned objects have been annotated
118102
with extra data or aggregations.
119103
"""
120104
+ self._add_origin()
121105
annotations = OrderedDict() # To preserve ordering of args
122106
for arg in args:
123107
# The default_alias property may raise a TypeError, so we use
124-
@@ -929,6 +964,7 @@
108+
@@ -960,6 +995,7 @@
125109
"""
126110
assert self.query.can_filter(), \
127111
"Cannot reorder a query once a slice has been taken."
128112
+ self._add_origin()
129113
obj = self._clone()
130114
obj.query.clear_ordering(force_empty=False)
131115
obj.query.add_ordering(*field_names)
132-
@@ -940,6 +976,7 @@
116+
@@ -971,6 +1007,7 @@
133117
"""
134118
assert self.query.can_filter(), \
135119
"Cannot create distinct fields once a slice has been taken."
136120
+ self._add_origin()
137121
obj = self._clone()
138122
obj.query.add_distinct_fields(*field_names)
139123
return obj
140-
@@ -951,6 +988,7 @@
124+
@@ -982,6 +1019,7 @@
141125
"""
142126
assert self.query.can_filter(), \
143127
"Cannot change a query once a slice has been taken"
144128
+ self._add_origin()
145129
clone = self._clone()
146130
clone.query.add_extra(select, select_params, where, params, tables, order_by)
147131
return clone
148-
@@ -959,6 +997,7 @@
132+
@@ -990,6 +1028,7 @@
149133
"""
150134
Reverses the ordering of the QuerySet.
151135
"""
152136
+ self._add_origin()
153137
clone = self._clone()
154138
clone.query.standard_ordering = not clone.query.standard_ordering
155139
return clone
156-
@@ -973,6 +1012,7 @@
140+
@@ -1004,6 +1043,7 @@
157141
"""
158142
if self._fields is not None:
159143
raise TypeError("Cannot call defer() after .values() or .values_list()")
160144
+ self._add_origin()
161145
clone = self._clone()
162146
if fields == (None,):
163147
clone.query.clear_deferred_loading()
164-
@@ -992,6 +1032,7 @@
148+
@@ -1023,6 +1063,7 @@
165149
# Can only pass None to defer(), not only(), as the rest option.
166150
# That won't stop people trying to do this, so let's be explicit.
167151
raise TypeError("Cannot pass None as an argument to only().")
168152
+ self._add_origin()
169153
clone = self._clone()
170154
clone.query.add_immediate_loading(fields)
171155
return clone
172-
@@ -1078,6 +1119,7 @@
156+
@@ -1109,6 +1150,7 @@
173157
clone._known_related_objects = self._known_related_objects
174158
clone._iterable_class = self._iterable_class
175159
clone._fields = self._fields
176160
+ clone._origin = self._origin
177161

178162
clone.__dict__.update(kwargs)
179163
return clone
180-
@@ -1085,6 +1127,8 @@
164+
@@ -1116,6 +1158,8 @@
181165
def _fetch_all(self):
182166
if self._result_cache is None:
183-
self._result_cache = list(self.iterator())
167+
self._result_cache = list(self._iterable_class(self))
184168
+ if settings.DEBUG:
185169
+ connections[self.db].queries_log[-1]['origin'] = self._origin
186170
if self._prefetch_related_lookups and not self._prefetch_done:

0 commit comments

Comments
 (0)