Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

magic-removal: Fixed #1350 -- Changed QuerySet.in_bulk() to return th…

…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...
commit 35f0dabdca98353bff9cb5e8b0ecae802fde80c2 1 parent f594bc4
@adrianholovaty adrianholovaty authored
Showing with 13 additions and 18 deletions.
  1. +13 −18 django/db/models/query.py
View
31 django/db/models/query.py
@@ -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.
@@ -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)
@@ -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().
Please sign in to comment.
Something went wrong with that request. Please try again.