Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made negative indexing on QuerySet instances raise an assertion error…

… (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...
commit 0c8ac0972c9b1a5af95d40334b83cf5f31d56331 1 parent b63157c
Luke Plant authored May 26, 2006
3  django/db/models/query.py
@@ -95,6 +95,9 @@ def __iter__(self):
95 95
 
96 96
     def __getitem__(self, k):
97 97
         "Retrieve an item or slice from the set of results."
  98
+        assert (not isinstance(k, slice) and (k >= 0)) \
  99
+            or (isinstance(k, slice) and (k.start is None or k.start >= 0) and (k.stop is None or k.stop >= 0)), \
  100
+            "Negative indexing is not supported."
98 101
         if self._result_cache is None:
99 102
             if isinstance(k, slice):
100 103
                 # Offset:
10  tests/modeltests/basic/models.py
@@ -283,6 +283,16 @@ def __repr__(self):
283 283
     ...
284 284
 AssertionError: Cannot combine queries once a slice has been taken.
285 285
 
  286
+# Negative slices are not supported, due to database constraints.
  287
+# (hint: inverting your ordering might do what you need).
  288
+>>> Article.objects.all()[-1]
  289
+Traceback (most recent call last):
  290
+    ...
  291
+AssertionError: Negative indexing is not supported.
  292
+>>> Article.objects.all()[0:-5]
  293
+Traceback (most recent call last):
  294
+    ...
  295
+AssertionError: Negative indexing is not supported.
286 296
 
287 297
 # An Article instance doesn't have access to the "objects" attribute.
288 298
 # That's only available on the class.

0 notes on commit 0c8ac09

Please sign in to comment.
Something went wrong with that request. Please try again.