Ticket #19895: 19895_1.diff

File 19895_1.diff, 3.8 KB (added by Grzegorz Nosek, 11 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 0f3a79a..4478c2a 100644
    a b class QuerySet(object):  
    302302        if fill_cache:
    303303            klass_info = get_klass_info(model, max_depth=max_depth,
    304304                                        requested=requested, only_load=only_load)
    305         for row in compiler.results_iter():
    306             if fill_cache:
    307                 obj, _ = get_cached_row(row, index_start, db, klass_info,
    308                                         offset=len(aggregate_select))
    309             else:
    310                 # Omit aggregates in object creation.
    311                 row_data = row[index_start:aggregate_start]
    312                 if skip:
    313                     obj = model_cls(**dict(zip(init_list, row_data)))
     305        try:
     306            for row in compiler.results_iter():
     307                if fill_cache:
     308                    obj, _ = get_cached_row(row, index_start, db, klass_info,
     309                                            offset=len(aggregate_select))
    314310                else:
    315                     obj = model(*row_data)
    316 
    317                 # Store the source database of the object
    318                 obj._state.db = db
    319                 # This object came from the database; it's not being added.
    320                 obj._state.adding = False
    321 
    322             if extra_select:
    323                 for i, k in enumerate(extra_select):
    324                     setattr(obj, k, row[i])
    325 
    326             # Add the aggregates to the model
    327             if aggregate_select:
    328                 for i, aggregate in enumerate(aggregate_select):
    329                     setattr(obj, aggregate, row[i + aggregate_start])
    330 
    331             # Add the known related objects to the model, if there are any
    332             if self._known_related_objects:
    333                 for field, rel_objs in self._known_related_objects.items():
    334                     pk = getattr(obj, field.get_attname())
    335                     try:
    336                         rel_obj = rel_objs[pk]
    337                     except KeyError:
    338                         pass               # may happen in qs1 | qs2 scenarios
     311                    # Omit aggregates in object creation.
     312                    row_data = row[index_start:aggregate_start]
     313                    if skip:
     314                        obj = model_cls(**dict(zip(init_list, row_data)))
    339315                    else:
    340                         setattr(obj, field.name, rel_obj)
    341 
    342             yield obj
     316                        obj = model(*row_data)
     317
     318                    # Store the source database of the object
     319                    obj._state.db = db
     320                    # This object came from the database; it's not being added.
     321                    obj._state.adding = False
     322
     323                if extra_select:
     324                    for i, k in enumerate(extra_select):
     325                        setattr(obj, k, row[i])
     326
     327                # Add the aggregates to the model
     328                if aggregate_select:
     329                    for i, aggregate in enumerate(aggregate_select):
     330                        setattr(obj, aggregate, row[i + aggregate_start])
     331
     332                # Add the known related objects to the model, if there are any
     333                if self._known_related_objects:
     334                    for field, rel_objs in self._known_related_objects.items():
     335                        pk = getattr(obj, field.get_attname())
     336                        try:
     337                            rel_obj = rel_objs[pk]
     338                        except KeyError:
     339                            pass               # may happen in qs1 | qs2 scenarios
     340                        else:
     341                            setattr(obj, field.name, rel_obj)
     342
     343                yield obj
     344        except Exception:
     345            self._result_cache = None
     346            raise
    343347
    344348    def aggregate(self, *args, **kwargs):
    345349        """
Back to Top