Ticket #8065: in_bulk.diff

File in_bulk.diff, 3.6 KB (added by ElliottM, 16 years ago)

patch with test and docs

  • django/db/models/query.py

     
    347347        obj.query.add_ordering('-%s' % latest_by)
    348348        return obj.get()
    349349
    350     def in_bulk(self, id_list):
     350    def in_bulk(self, id_list=None):
    351351        """
    352352        Returns a dictionary mapping each of the given IDs to the object with
    353353        that ID.
    354354        """
    355355        assert self.query.can_filter(), \
    356356                "Cannot use 'limit' or 'offset' with in_bulk"
    357         assert isinstance(id_list, (tuple,  list)), \
    358                 "in_bulk() must be provided with a list of IDs."
    359         if not id_list:
    360             return {}
    361         qs = self._clone()
    362         qs.query.add_filter(('pk__in', id_list))
     357        if id_list is not None:
     358            assert isinstance(id_list, (tuple,  list)), \
     359                    "in_bulk() must be provided with a list of IDs."
     360            if not id_list:
     361                return {}
     362            qs = self._clone()
     363            qs.query.add_filter(('pk__in', id_list))
     364        else:
     365            qs = self._clone()
     366       
    363367        return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()])
    364368
    365369    def delete(self):
  • tests/modeltests/lookup/models.py

     
    1919__test__ = {'API_TESTS':r"""
    2020# Create a couple of Articles.
    2121>>> from datetime import datetime
     22>>> try:
     23...     empty_list = sorted([])
     24... except NameError:
     25...     from django.utils.itercompat import sorted
     26
    2227>>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26))
    2328>>> a1.save()
    2429>>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27))
     
    9196Traceback (most recent call last):
    9297    ...
    9398AssertionError: in_bulk() must be provided with a list of IDs.
    94 >>> Article.objects.in_bulk()
    95 Traceback (most recent call last):
    96     ...
    97 TypeError: in_bulk() takes exactly 2 arguments (1 given)
    9899>>> Article.objects.in_bulk(headline__startswith='Blah')
    99100Traceback (most recent call last):
    100101    ...
    101102TypeError: in_bulk() got an unexpected keyword argument 'headline__startswith'
    102103
     104#if in_bulk() is called with no arguments, the entire queryset is evaluated
     105>>> sorted(Article.objects.in_bulk().items())
     106[(1, <Article: Article 1>), (2, <Article: Article 2>), (3, <Article: Article 3>), (4, <Article: Article 4>), (5, <Article: Article 5>), (6, <Article: Article 6>), (7, <Article: Article 7>)]
     107>>> sorted(Article.objects.filter(pub_date=datetime(2005, 7, 27)).in_bulk().items())
     108[(2, <Article: Article 2>), (3, <Article: Article 3>), (7, <Article: Article 7>)]
     109
     110
     111
    103112# values() returns a list of dictionaries instead of object instances -- and
    104113# you can specify which fields you want to retrieve.
    105114>>> Article.objects.values('headline')
  • docs/db-api.txt

     
    12051205is an underlying implementation quirk that shouldn't pose any real-world
    12061206problems.
    12071207
    1208 ``in_bulk(id_list)``
     1208``in_bulk(id_list=None)``
    12091209~~~~~~~~~~~~~~~~~~~~
    12101210
    12111211Takes a list of primary-key values and returns a dictionary mapping each
     
    12221222
    12231223If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary.
    12241224
     1225**New in Django development version** If you do not pass ``in_bulk()`` any
     1226arguments, it will return all of the objects in the queryset.
     1227
    12251228``iterator()``
    12261229~~~~~~~~~~~~~~
    12271230
Back to Top