Code

Ticket #19895: 19895_1.diff

File 19895_1.diff, 3.8 KB (added by gnosek, 14 months ago)
Line 
1diff --git a/django/db/models/query.py b/django/db/models/query.py
2index 0f3a79a..4478c2a 100644
3--- a/django/db/models/query.py
4+++ b/django/db/models/query.py
5@@ -302,44 +302,48 @@ class QuerySet(object):
6         if fill_cache:
7             klass_info = get_klass_info(model, max_depth=max_depth,
8                                         requested=requested, only_load=only_load)
9-        for row in compiler.results_iter():
10-            if fill_cache:
11-                obj, _ = get_cached_row(row, index_start, db, klass_info,
12-                                        offset=len(aggregate_select))
13-            else:
14-                # Omit aggregates in object creation.
15-                row_data = row[index_start:aggregate_start]
16-                if skip:
17-                    obj = model_cls(**dict(zip(init_list, row_data)))
18+        try:
19+            for row in compiler.results_iter():
20+                if fill_cache:
21+                    obj, _ = get_cached_row(row, index_start, db, klass_info,
22+                                            offset=len(aggregate_select))
23                 else:
24-                    obj = model(*row_data)
25-
26-                # Store the source database of the object
27-                obj._state.db = db
28-                # This object came from the database; it's not being added.
29-                obj._state.adding = False
30-
31-            if extra_select:
32-                for i, k in enumerate(extra_select):
33-                    setattr(obj, k, row[i])
34-
35-            # Add the aggregates to the model
36-            if aggregate_select:
37-                for i, aggregate in enumerate(aggregate_select):
38-                    setattr(obj, aggregate, row[i + aggregate_start])
39-
40-            # Add the known related objects to the model, if there are any
41-            if self._known_related_objects:
42-                for field, rel_objs in self._known_related_objects.items():
43-                    pk = getattr(obj, field.get_attname())
44-                    try:
45-                        rel_obj = rel_objs[pk]
46-                    except KeyError:
47-                        pass               # may happen in qs1 | qs2 scenarios
48+                    # Omit aggregates in object creation.
49+                    row_data = row[index_start:aggregate_start]
50+                    if skip:
51+                        obj = model_cls(**dict(zip(init_list, row_data)))
52                     else:
53-                        setattr(obj, field.name, rel_obj)
54-
55-            yield obj
56+                        obj = model(*row_data)
57+
58+                    # Store the source database of the object
59+                    obj._state.db = db
60+                    # This object came from the database; it's not being added.
61+                    obj._state.adding = False
62+
63+                if extra_select:
64+                    for i, k in enumerate(extra_select):
65+                        setattr(obj, k, row[i])
66+
67+                # Add the aggregates to the model
68+                if aggregate_select:
69+                    for i, aggregate in enumerate(aggregate_select):
70+                        setattr(obj, aggregate, row[i + aggregate_start])
71+
72+                # Add the known related objects to the model, if there are any
73+                if self._known_related_objects:
74+                    for field, rel_objs in self._known_related_objects.items():
75+                        pk = getattr(obj, field.get_attname())
76+                        try:
77+                            rel_obj = rel_objs[pk]
78+                        except KeyError:
79+                            pass               # may happen in qs1 | qs2 scenarios
80+                        else:
81+                            setattr(obj, field.name, rel_obj)
82+
83+                yield obj
84+        except Exception:
85+            self._result_cache = None
86+            raise
87 
88     def aggregate(self, *args, **kwargs):
89         """