Ticket #8065: in_bulk.diff
File in_bulk.diff, 3.6 KB (added by , 16 years ago) |
---|
-
django/db/models/query.py
347 347 obj.query.add_ordering('-%s' % latest_by) 348 348 return obj.get() 349 349 350 def in_bulk(self, id_list ):350 def in_bulk(self, id_list=None): 351 351 """ 352 352 Returns a dictionary mapping each of the given IDs to the object with 353 353 that ID. 354 354 """ 355 355 assert self.query.can_filter(), \ 356 356 "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 363 367 return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()]) 364 368 365 369 def delete(self): -
tests/modeltests/lookup/models.py
19 19 __test__ = {'API_TESTS':r""" 20 20 # Create a couple of Articles. 21 21 >>> from datetime import datetime 22 >>> try: 23 ... empty_list = sorted([]) 24 ... except NameError: 25 ... from django.utils.itercompat import sorted 26 22 27 >>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26)) 23 28 >>> a1.save() 24 29 >>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27)) … … 91 96 Traceback (most recent call last): 92 97 ... 93 98 AssertionError: 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)98 99 >>> Article.objects.in_bulk(headline__startswith='Blah') 99 100 Traceback (most recent call last): 100 101 ... 101 102 TypeError: in_bulk() got an unexpected keyword argument 'headline__startswith' 102 103 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 103 112 # values() returns a list of dictionaries instead of object instances -- and 104 113 # you can specify which fields you want to retrieve. 105 114 >>> Article.objects.values('headline') -
docs/db-api.txt
1205 1205 is an underlying implementation quirk that shouldn't pose any real-world 1206 1206 problems. 1207 1207 1208 ``in_bulk(id_list )``1208 ``in_bulk(id_list=None)`` 1209 1209 ~~~~~~~~~~~~~~~~~~~~ 1210 1210 1211 1211 Takes a list of primary-key values and returns a dictionary mapping each … … 1222 1222 1223 1223 If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary. 1224 1224 1225 **New in Django development version** If you do not pass ``in_bulk()`` any 1226 arguments, it will return all of the objects in the queryset. 1227 1225 1228 ``iterator()`` 1226 1229 ~~~~~~~~~~~~~~ 1227 1230