Django

Code

Changeset 2992

Show
Ignore:
Timestamp:
05/26/06 13:41:03 (2 years ago)
Author:
lukeplant
Message:

Made negative indexing on QuerySet? instances raise an assertion error (previously
it just returned incorrect results).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/query.py

    r2970 r2992  
    9696    def __getitem__(self, k): 
    9797        "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." 
    98101        if self._result_cache is None: 
    99102            if isinstance(k, slice): 
  • django/trunk/tests/modeltests/basic/models.py

    r2898 r2992  
    284284AssertionError: Cannot combine queries once a slice has been taken. 
    285285 
     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] 
     289Traceback (most recent call last): 
     290    ... 
     291AssertionError: Negative indexing is not supported. 
     292>>> Article.objects.all()[0:-5] 
     293Traceback (most recent call last): 
     294    ... 
     295AssertionError: Negative indexing is not supported. 
    286296 
    287297# An Article instance doesn't have access to the "objects" attribute.