Ticket #17439: 17439.patch
File 17439.patch, 2.9 KB (added by , 13 years ago) |
---|
-
tests/modeltests/prefetch_related/tests.py
68 68 69 69 self.assertQuerysetEqual(self.book2.authors.all(), [u"<Author: Charlotte>"]) 70 70 71 def test_onetoone_reverse_no_match(self): 72 # Regression for #17439 73 with self.assertNumQueries(2): 74 book = Book.objects.prefetch_related('bookwithyear').all()[0] 75 with self.assertNumQueries(0): 76 with self.assertRaises(BookWithYear.DoesNotExist): 77 book.bookwithyear 78 71 79 def test_survives_clone(self): 72 80 with self.assertNumQueries(2): 73 81 lists = [list(b.first_time_authors.all()) -
django/db/models/query.py
6 6 import itertools 7 7 import sys 8 8 9 from django.core import exceptions 9 10 from django.db import connections, router, transaction, IntegrityError 10 11 from django.db.models.fields import AutoField 11 12 from django.db.models.query_utils import (Q, select_related_descend, … … 1677 1678 # (e.g. via select_related), or hopefully some other property 1678 1679 # that doesn't support prefetching but needs to be traversed. 1679 1680 1680 # We replace the current list of parent objects with that list. 1681 obj_list = [getattr(obj, attr) for obj in obj_list] 1681 # We replace the current list of parent objects with that list, 1682 # filtering out empty or missing values so that we can continue 1683 # with nullable or reverse relations. 1684 new_obj_list = [] 1685 for obj in obj_list: 1686 try: 1687 new_obj = getattr(obj, attr) 1688 except exceptions.ObjectDoesNotExist: 1689 continue 1690 if new_obj is None: 1691 continue 1692 new_obj_list.append(new_obj) 1693 obj_list = new_obj_list 1682 1694 1683 # Filter out 'None' so that we can continue with nullable1684 # relations.1685 obj_list = [obj for obj in obj_list if obj is not None]1686 1695 1687 1688 1696 def get_prefetcher(instance, attr): 1689 1697 """ 1690 1698 For the attribute 'attr' on the given instance, finds … … 1778 1786 vals = rel_obj_cache.get(instance_attr_val, []) 1779 1787 if single: 1780 1788 # Need to assign to single cache on instance 1781 if vals: 1782 setattr(obj, cache_name, vals[0]) 1789 setattr(obj, cache_name, vals[0] if vals else None) 1783 1790 else: 1784 1791 # Multi, attribute represents a manager with an .all() method that 1785 1792 # returns a QuerySet