Loading django/db/models/query.py +8 −5 Original line number Diff line number Diff line Loading @@ -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 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): Loading docs/ref/models/querysets.txt +9 −2 Original line number Diff line number Diff line Loading @@ -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:: Loading @@ -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 ~~~~~~~~ Loading docs/releases/1.10.txt +3 −0 Original line number Diff line number Diff line Loading @@ -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 ^^^^^^^^^^^^^^^^^^^^^^ Loading tests/lookup/tests.py +12 −1 Original line number Diff line number Diff line Loading @@ -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}) Loading @@ -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): Loading Loading
django/db/models/query.py +8 −5 Original line number Diff line number Diff line Loading @@ -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 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): Loading
docs/ref/models/querysets.txt +9 −2 Original line number Diff line number Diff line Loading @@ -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:: Loading @@ -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 ~~~~~~~~ Loading
docs/releases/1.10.txt +3 −0 Original line number Diff line number Diff line Loading @@ -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 ^^^^^^^^^^^^^^^^^^^^^^ Loading
tests/lookup/tests.py +12 −1 Original line number Diff line number Diff line Loading @@ -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}) Loading @@ -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): Loading