From 049212e95079d612b37b6fb9f3fba8616f5f7a7b Mon Sep 17 00:00:00 2001 From: Gary Wilson Jr Date: Wed, 8 Aug 2007 21:09:55 +0000 Subject: [PATCH] Fixed #5115 -- Fixed `QuerySet` slices to allow longs. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5831 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 2 +- tests/modeltests/basic/models.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index f5213ba903..7620c366ce 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -114,7 +114,7 @@ class _QuerySet(object): def __getitem__(self, k): "Retrieve an item or slice from the set of results." - if not isinstance(k, (slice, int)): + if not isinstance(k, (slice, int, long)): raise TypeError 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)), \ diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py index d2220320a0..0a09579761 100644 --- a/tests/modeltests/basic/models.py +++ b/tests/modeltests/basic/models.py @@ -247,6 +247,19 @@ datetime.datetime(2005, 7, 28, 0, 0) >>> (s1 | s2 | s3)[::2] [, ] +# Slicing works with longs. +>>> Article.objects.all()[0L] + +>>> Article.objects.all()[1L:3L] +[, ] +>>> s3 = Article.objects.filter(id__exact=3) +>>> (s1 | s2 | s3)[::2L] +[, ] + +# And can be mixed with ints. +>>> Article.objects.all()[1:3L] +[, ] + # Slices (without step) are lazy: >>> Article.objects.all()[0:5].filter() [, , , , ]