mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #8065 -- Made id_list an optional argument for QuerySet.in_bulk().
This commit is contained in:
parent
2a7ce34600
commit
62ca2dea04
@ -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):
|
||||
|
@ -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
|
||||
~~~~~~~~
|
||||
|
||||
|
@ -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
|
||||
^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user