From 9708aa51c0d90a60c9b71aa0c0f80208c95be477 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Mon, 30 Jan 2006 03:33:40 +0000 Subject: [PATCH] magic-removal: Restored __getitem__() on QuerySet and fixed bug in QuerySet._clone() git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2167 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 00c17b1ba1..c2e6e1808c 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -90,6 +90,20 @@ class QuerySet(object): def __iter__(self): return iter(self._get_data()) + def __getitem__(self, k): + "Retrieve an item or slice from the set of results." + # __getitem__ can't return QuerySet instances, because filter() and + # order_by() on the result would break badly. This means we don't have + # to worry about arithmetic with self._limit or self._offset -- they'll + # both be None at this point. + if self._result_cache is None: + if isinstance(k, slice): + return list(self._clone(_offset=k.start, _limit=k.stop))[::k.step] + else: + return self._clone(_offset=k, _limit=1).get() + else: + return self._result_cache[k] + #################################### # METHODS THAT DO DATABASE QUERIES # #################################### @@ -205,6 +219,7 @@ class QuerySet(object): c._tables = self._tables c._offset = self._offset c._limit = self._limit + c.__dict__.update(kwargs) return c def _get_data(self): @@ -319,30 +334,6 @@ class QuerySet(object): # combined = self._combine(other) # combined._filter = self._filter | other._filter # return combined -# -# def __getitem__(self, k): -# """Retrieve an item or slice from the set of results""" -# # getitem can't return query instances, because .filter() -# # and .order_by() methods on the result would break badly. -# # This means we don't have to worry about arithmetic with -# # self._limit or self._offset - they will both be None -# # at this point -# if isinstance(k, slice): -# # Get a new query if we haven't already got data from db -# if self._result_cache is None: -# # slice.stop and slice.start -# clone = self._clone(_offset=k.start, _limit=k.stop) -# return list(clone)[::k.step] -# # TODO - we are throwing away this retrieved data. -# # We could cache it if we had some kind of sparse -# # list structure we could put it in. -# else: -# return self._result_cache[k] -# -# else: -# # TODO: possibly use a new query which just gets one item -# # if we haven't already got them all? -# return self._fetch_data()[k] class QOperator: "Base class for QAnd and QOr"