Skip to content

Commit

Permalink
Made negative indexing on QuerySet instances raise an assertion error…
Browse files Browse the repository at this point in the history
… (previously

it just returned incorrect results).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@2992 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
spookylukey committed May 26, 2006
1 parent b63157c commit 0c8ac09
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions django/db/models/query.py
Expand Up @@ -95,6 +95,9 @@ def __iter__(self):


def __getitem__(self, k): def __getitem__(self, k):
"Retrieve an item or slice from the set of results." "Retrieve an item or slice from the set of results."
assert (not isinstance(k, slice) and (k >= 0)) \
or (isinstance(k, slice) and (k.start is None or k.start >= 0) and (k.stop is None or k.stop >= 0)), \
"Negative indexing is not supported."
if self._result_cache is None: if self._result_cache is None:
if isinstance(k, slice): if isinstance(k, slice):
# Offset: # Offset:
Expand Down
10 changes: 10 additions & 0 deletions tests/modeltests/basic/models.py
Expand Up @@ -283,6 +283,16 @@ def __repr__(self):
... ...
AssertionError: Cannot combine queries once a slice has been taken. AssertionError: Cannot combine queries once a slice has been taken.
# Negative slices are not supported, due to database constraints.
# (hint: inverting your ordering might do what you need).
>>> Article.objects.all()[-1]
Traceback (most recent call last):
...
AssertionError: Negative indexing is not supported.
>>> Article.objects.all()[0:-5]
Traceback (most recent call last):
...
AssertionError: Negative indexing is not supported.
# An Article instance doesn't have access to the "objects" attribute. # An Article instance doesn't have access to the "objects" attribute.
# That's only available on the class. # That's only available on the class.
Expand Down

0 comments on commit 0c8ac09

Please sign in to comment.