Skip to content

Commit

Permalink
magic-removal: Fixed #1350 -- Changed QuerySet.in_bulk() to return th…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
adrianholovaty committed Feb 18, 2006
1 parent f594bc4 commit 35f0dab
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ def latest(self, field_name=None):
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.
Expand Down Expand Up @@ -207,12 +220,6 @@ def delete(self):
# 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)

Expand Down Expand Up @@ -380,18 +387,6 @@ def _get_sql_clause(self):

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().
Expand Down

0 comments on commit 35f0dab

Please sign in to comment.