Author: lukeplant
Date: 2006-05-26 13:41:03 -0500 (Fri, 26 May 2006)
New Revision: 2992

Modified:
   django/trunk/django/db/models/query.py
   django/trunk/tests/modeltests/basic/models.py
Log:
Made negative indexing on QuerySet instances raise an assertion error 
(previously
it just returned incorrect results).


Modified: django/trunk/django/db/models/query.py
===================================================================
--- django/trunk/django/db/models/query.py      2006-05-26 17:54:19 UTC (rev 
2991)
+++ django/trunk/django/db/models/query.py      2006-05-26 18:41:03 UTC (rev 
2992)
@@ -95,6 +95,9 @@
 
     def __getitem__(self, k):
         "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 isinstance(k, slice):
                 # Offset:

Modified: django/trunk/tests/modeltests/basic/models.py
===================================================================
--- django/trunk/tests/modeltests/basic/models.py       2006-05-26 17:54:19 UTC 
(rev 2991)
+++ django/trunk/tests/modeltests/basic/models.py       2006-05-26 18:41:03 UTC 
(rev 2992)
@@ -283,6 +283,16 @@
     ...
 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.
 # That's only available on the class.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates
-~----------~----~----~----~------~----~------~--~---

Reply via email to