magic-removal: Fixed #1350 -- Changed QuerySet.in_bulk() to return the dictionary directly rather than returning a QuerySet subclass.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2354 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-02-18 22:47:03 +00:00
parent f594bc4445
commit 35f0dabdca
1 changed files with 13 additions and 18 deletions

View File

@ -178,6 +178,19 @@ class QuerySet(object):
assert bool(latest_by), "latest() requires either a field_name parameter or 'get_latest_by' in the model"
return self._clone(_limit=1, _order_by=('-'+latest_by,)).get()
def in_bulk(self, id_list):
"""
Returns a dictionary mapping each of the given IDs to the object with
that ID.
"""
assert isinstance(id_list, (tuple, list)), "in_bulk() must be provided with a list of IDs."
id_list = list(id_list)
assert id_list != [], "in_bulk() cannot be passed an empty ID list."
qs = self._clone()
qs._where.append("%s.%s IN (%s)" % (backend.quote_name(self.model._meta.db_table), backend.quote_name(self.model._meta.pk.column), ",".join(['%s'] * len(id_list))))
qs._params.extend(id_list)
return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()])
def delete(self):
"""
Deletes the records in the current QuerySet.
@ -207,12 +220,6 @@ class QuerySet(object):
# PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS #
##################################################
def in_bulk(self, id_list):
assert isinstance(id_list, (tuple, list)), "in_bulk() must be provided with a list of IDs."
id_list = list(id_list)
assert id_list != [], "in_bulk() cannot be passed an empty ID list."
return self._clone(klass=InBulkQuerySet, _id_list=id_list)
def values(self, *fields):
return self._clone(klass=ValuesQuerySet, _fields=fields)
@ -380,18 +387,6 @@ class QuerySet(object):
return select, " ".join(sql), params
class InBulkQuerySet(QuerySet):
def iterator(self):
self._where.append("%s.%s IN (%s)" % (backend.quote_name(self.model._meta.db_table), backend.quote_name(self.model._meta.pk.column), ",".join(['%s'] * len(self._id_list))))
self._params.extend(self._id_list)
yield dict([(obj._get_pk_val(), obj) for obj in QuerySet.iterator(self)])
def _get_data(self):
if self._result_cache is None:
for i in self.iterator():
self._result_cache = i
return self._result_cache
class ValuesQuerySet(QuerySet):
def iterator(self):
# select_related and select aren't supported in values().