MCPcopy Index your code
hub / github.com/django-haystack/django-haystack / post_process_results

Method post_process_results

haystack/query.py:155–203  ·  view source on GitHub ↗
(self, results)

Source from the content-addressed store, hash-verified

153 return
154
155 def post_process_results(self, results):
156 to_cache = []
157
158 # Check if we wish to load all objects.
159 if self._load_all:
160 models_pks = {}
161 loaded_objects = {}
162
163 # Remember the search position for each result so we don't have to resort later.
164 for result in results:
165 models_pks.setdefault(result.model, []).append(result.pk)
166
167 # Load the objects for each model in turn.
168 for model in models_pks:
169 loaded_objects[model] = self._load_model_objects(
170 model, models_pks[model]
171 )
172
173 for result in results:
174 if self._load_all:
175 model_objects = loaded_objects.get(result.model, {})
176 # Try to coerce a primary key object that matches the models pk
177 # We have to deal with semi-arbitrary keys being cast from strings (UUID, int, etc)
178 if model_objects:
179 result_klass = type(next(iter(model_objects)))
180 result.pk = result_klass(result.pk)
181
182 try:
183 result._object = model_objects[result.pk]
184 except KeyError:
185 # The object was either deleted since we indexed or should
186 # be ignored for other reasons such as an overriden 'load_all_queryset';
187 # fail silently.
188 self._ignored_result_count += 1
189
190 # avoid an unfilled None at the end of the result cache
191 self._result_cache.pop()
192 continue
193 else:
194 # No objects were returned -- possible due to SQS nesting such as
195 # XYZ.objects.filter(id__gt=10) where the amount ignored are
196 # exactly equal to the ITERATOR_LOAD_PER_QUERY
197 del self._result_cache[:1]
198 self._ignored_result_count += 1
199 continue
200
201 to_cache.append(result)
202
203 return to_cache
204
205 def _load_model_objects(self, model, pks):
206 try:

Callers 1

_fill_cacheMethod · 0.95

Calls 2

_load_model_objectsMethod · 0.95
getMethod · 0.80

Tested by

no test coverage detected