Author: lukeplant
Date: 2012-02-28 11:34:04 -0800 (Tue, 28 Feb 2012)
New Revision: 17600

Modified:
   django/trunk/django/db/models/query.py
   django/trunk/docs/ref/models/querysets.txt
   django/trunk/tests/modeltests/prefetch_related/tests.py
Log:
Fixed #17668 - prefetch_related does not work in in_bulk

Thanks to gurets for the report, and akaariai for the initial patch.

Modified: django/trunk/django/db/models/query.py
===================================================================
--- django/trunk/django/db/models/query.py      2012-02-28 09:50:18 UTC (rev 
17599)
+++ django/trunk/django/db/models/query.py      2012-02-28 19:34:04 UTC (rev 
17600)
@@ -485,7 +485,7 @@
         qs = self._clone()
         qs.query.add_filter(('pk__in', id_list))
         qs.query.clear_ordering(force_empty=True)
-        return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()])
+        return dict([(obj._get_pk_val(), obj) for obj in qs])
 
     def delete(self):
         """

Modified: django/trunk/docs/ref/models/querysets.txt
===================================================================
--- django/trunk/docs/ref/models/querysets.txt  2012-02-28 09:50:18 UTC (rev 
17599)
+++ django/trunk/docs/ref/models/querysets.txt  2012-02-28 19:34:04 UTC (rev 
17600)
@@ -871,6 +871,9 @@
 problems of its own when it comes to parsing or executing the SQL query. Always
 profile for your use case!
 
+Note that if you use ``iterator()`` to run the query, ``prefetch_related()``
+calls will be ignored since these two optimizations do not make sense together.
+
 extra
 ~~~~~
 
@@ -1430,6 +1433,9 @@
 Note that using ``iterator()`` on a ``QuerySet`` which has already been
 evaluated will force it to evaluate again, repeating the query.
 
+Also, use of ``iterator()`` causes previous ``prefetch_related()`` calls to be
+ignored since these two optimizations do not make sense together.
+
 latest
 ~~~~~~
 

Modified: django/trunk/tests/modeltests/prefetch_related/tests.py
===================================================================
--- django/trunk/tests/modeltests/prefetch_related/tests.py     2012-02-28 
09:50:18 UTC (rev 17599)
+++ django/trunk/tests/modeltests/prefetch_related/tests.py     2012-02-28 
19:34:04 UTC (rev 17600)
@@ -470,3 +470,16 @@
                         for e in qs2]
 
         self.assertEqual(co_serfs, co_serfs2)
+
+    def test_in_bulk(self):
+        """
+        In-bulk does correctly prefetch objects by not using .iterator()
+        directly.
+        """
+        boss1 = Employee.objects.create(name="Peter")
+        boss2 = Employee.objects.create(name="Jack")
+        with self.assertNumQueries(2):
+            # Check that prefetch is done and it does not cause any errors.
+            bulk = 
Employee.objects.prefetch_related('serfs').in_bulk([boss1.pk, boss2.pk])
+            for b in bulk.values():
+                list(b.serfs.all())

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to