Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
commit a666b987835e99c55f51cd2feaf287111cbfcd86 1 parent 2326928
Luke Plant authored May 23, 2006
4  django/db/models/base.py
@@ -269,8 +269,8 @@ def _get_next_or_previous_by_FIELD(self, field, is_next):
269 269
         q._params.extend([param, param, getattr(self, self._meta.pk.attname)])
270 270
         try:
271 271
             return q[0]
272  
-        except IndexError, e:
273  
-            raise self.DoesNotExist, e.args
  272
+        except IndexError:
  273
+            raise self.DoesNotExist, "%s matching query does not exist." % self.__class__._meta.object_name
274 274
 
275 275
     def _get_next_or_previous_in_order(self, is_next):
276 276
         cachename = "__%s_order_cache" % is_next
3  django/db/models/query.py
@@ -129,7 +129,7 @@ def __getitem__(self, k):
129 129
                     return list(self._clone(_offset=offset, _limit=limit))[::k.step]
130 130
             else:
131 131
                 try:
132  
-                    return self._clone(_offset=k, _limit=1).get()
  132
+                    return list(self._clone(_offset=k, _limit=1))[0]
133 133
                 except self.model.DoesNotExist, e:
134 134
                     raise IndexError, e.args
135 135
         else:
@@ -193,6 +193,7 @@ def count(self):
193 193
     def get(self, *args, **kwargs):
194 194
         "Performs the SELECT and returns a single object matching the given keyword arguments."
195 195
         clone = self.filter(*args, **kwargs)
  196
+        # clean up SQL by removing unneeded ORDER BY
196 197
         if not clone._order_by:
197 198
             clone._order_by = ()
198 199
         obj_list = list(clone)
4  tests/modeltests/ordering/models.py
@@ -56,6 +56,10 @@ def __repr__(self):
56 56
 >>> Article.objects.order_by('headline')[1:3]
57 57
 [Article 2, Article 3]
58 58
 
  59
+# Getting a single item should work too:
  60
+>>> Article.objects.all()[0]
  61
+Article 4
  62
+
59 63
 # Use '?' to order randomly. (We're using [...] in the output to indicate we
60 64
 # don't know what order the output will be in.
61 65
 >>> Article.objects.order_by('?')

0 notes on commit a666b98

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