Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug with Meta.ordering being ignored when slicing a single item…

… off a QuerySet. Thanks, Gábor Fawkes.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2970 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a666b987835e99c55f51cd2feaf287111cbfcd86 1 parent 2326928
@spookylukey spookylukey authored
View
4 django/db/models/base.py
@@ -269,8 +269,8 @@ def _get_next_or_previous_by_FIELD(self, field, is_next):
q._params.extend([param, param, getattr(self, self._meta.pk.attname)])
try:
return q[0]
- except IndexError, e:
- raise self.DoesNotExist, e.args
+ except IndexError:
+ raise self.DoesNotExist, "%s matching query does not exist." % self.__class__._meta.object_name
def _get_next_or_previous_in_order(self, is_next):
cachename = "__%s_order_cache" % is_next
View
3  django/db/models/query.py
@@ -129,7 +129,7 @@ def __getitem__(self, k):
return list(self._clone(_offset=offset, _limit=limit))[::k.step]
else:
try:
- return self._clone(_offset=k, _limit=1).get()
+ return list(self._clone(_offset=k, _limit=1))[0]
except self.model.DoesNotExist, e:
raise IndexError, e.args
else:
@@ -193,6 +193,7 @@ def count(self):
def get(self, *args, **kwargs):
"Performs the SELECT and returns a single object matching the given keyword arguments."
clone = self.filter(*args, **kwargs)
+ # clean up SQL by removing unneeded ORDER BY
if not clone._order_by:
clone._order_by = ()
obj_list = list(clone)
View
4 tests/modeltests/ordering/models.py
@@ -56,6 +56,10 @@ def __repr__(self):
>>> Article.objects.order_by('headline')[1:3]
[Article 2, Article 3]
+# Getting a single item should work too:
+>>> Article.objects.all()[0]
+Article 4
+
# Use '?' to order randomly. (We're using [...] in the output to indicate we
# don't know what order the output will be in.
>>> Article.objects.order_by('?')
Please sign in to comment.
Something went wrong with that request. Please try again.