summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Marty <bryanmarty@gmail.com>2015-10-29 23:24:46 -0700
committerTim Graham <timograham@gmail.com>2015-12-26 17:57:19 -0500
commit62ca2dea04489bdb23df1c7815439287c6d44630 (patch)
treeeb12cc7fef04d1bce3e1a3f6b1c14124884f6da0
parent2a7ce34600d0f879e93c9a5e02215948ed3bb6ac (diff)
Fixed #8065 -- Made id_list an optional argument for QuerySet.in_bulk().
-rw-r--r--django/db/models/query.py13
-rw-r--r--docs/ref/models/querysets.txt11
-rw-r--r--docs/releases/1.10.txt3
-rw-r--r--tests/lookup/tests.py13
4 files changed, 32 insertions, 8 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index e8d9006b48..45c0320254 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -559,16 +559,19 @@ class QuerySet(object):
return objects[0]
return None
- def in_bulk(self, id_list):
+ def in_bulk(self, id_list=None):
"""
Returns a dictionary mapping each of the given IDs to the object with
- that ID.
+ that ID. If `id_list` isn't provided, the entire QuerySet is evaluated.
"""
assert self.query.can_filter(), \
"Cannot use 'limit' or 'offset' with in_bulk"
- if not id_list:
- return {}
- qs = self.filter(pk__in=id_list).order_by()
+ if id_list is not None:
+ if not id_list:
+ return {}
+ qs = self.filter(pk__in=id_list).order_by()
+ else:
+ qs = self._clone()
return {obj._get_pk_val(): obj for obj in qs}
def delete(self):
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
index c9c3712669..af307fa47d 100644
--- a/docs/ref/models/querysets.txt
+++ b/docs/ref/models/querysets.txt
@@ -1836,10 +1836,11 @@ database query like ``count()`` would.
in_bulk
~~~~~~~
-.. method:: in_bulk(id_list)
+.. method:: in_bulk(id_list=None)
Takes a list of primary-key values and returns a dictionary mapping each
-primary-key value to an instance of the object with the given ID.
+primary-key value to an instance of the object with the given ID. If a list
+isn't provided, all objects in the queryset are returned.
Example::
@@ -1849,9 +1850,15 @@ Example::
{1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>}
>>> Blog.objects.in_bulk([])
{}
+ >>> Blog.objects.in_bulk()
+ {1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>, 3: <Blog: Django Weblog>}
If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary.
+.. versionchanged:: 1.10
+
+ In older versions, ``id_list`` was a required argument.
+
iterator
~~~~~~~~
diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt
index 077a27cea1..31e15663ba 100644
--- a/docs/releases/1.10.txt
+++ b/docs/releases/1.10.txt
@@ -246,6 +246,9 @@ Models
:class:`~django.db.models.AutoField` except that it is guaranteed
to fit numbers from ``1`` to ``9223372036854775807``.
+* :meth:`QuerySet.in_bulk() <django.db.models.query.QuerySet.in_bulk>`
+ may be called without any arguments to return all objects in the queryset.
+
Requests and Responses
^^^^^^^^^^^^^^^^^^^^^^
diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py
index 7e9f0921f8..203f25406a 100644
--- a/tests/lookup/tests.py
+++ b/tests/lookup/tests.py
@@ -116,6 +116,18 @@ class LookupTests(TestCase):
arts = Article.objects.in_bulk([self.a1.id, self.a2.id])
self.assertEqual(arts[self.a1.id], self.a1)
self.assertEqual(arts[self.a2.id], self.a2)
+ self.assertEqual(
+ Article.objects.in_bulk(),
+ {
+ self.a1.id: self.a1,
+ self.a2.id: self.a2,
+ self.a3.id: self.a3,
+ self.a4.id: self.a4,
+ self.a5.id: self.a5,
+ self.a6.id: self.a6,
+ self.a7.id: self.a7,
+ }
+ )
self.assertEqual(Article.objects.in_bulk([self.a3.id]), {self.a3.id: self.a3})
self.assertEqual(Article.objects.in_bulk({self.a3.id}), {self.a3.id: self.a3})
self.assertEqual(Article.objects.in_bulk(frozenset([self.a3.id])), {self.a3.id: self.a3})
@@ -124,7 +136,6 @@ class LookupTests(TestCase):
self.assertEqual(Article.objects.in_bulk([]), {})
self.assertEqual(Article.objects.in_bulk(iter([self.a1.id])), {self.a1.id: self.a1})
self.assertEqual(Article.objects.in_bulk(iter([])), {})
- self.assertRaises(TypeError, Article.objects.in_bulk)
self.assertRaises(TypeError, Article.objects.in_bulk, headline__startswith='Blah')
def test_values(self):