1
0
mirror of https://github.com/django/django.git synced 2025-10-29 00:26:07 +00:00

Fixed #23555 -- Avoided suppressing IndexError in QuerySet.first() and .last()

This commit is contained in:
Artem Rizhov
2014-10-08 18:27:17 +03:00
committed by Tim Graham
parent 9e2e4cb6dd
commit ca61195827
3 changed files with 48 additions and 11 deletions

View File

@@ -516,21 +516,19 @@ class QuerySet(object):
"""
Returns the first object of a query, returns None if no match is found.
"""
qs = self if self.ordered else self.order_by('pk')
try:
return qs[0]
except IndexError:
return None
objects = list((self if self.ordered else self.order_by('pk'))[:1])
if objects:
return objects[0]
return None
def last(self):
"""
Returns the last object of a query, returns None if no match is found.
"""
qs = self.reverse() if self.ordered else self.order_by('-pk')
try:
return qs[0]
except IndexError:
return None
objects = list((self.reverse() if self.ordered else self.order_by('-pk'))[:1])
if objects:
return objects[0]
return None
def in_bulk(self, id_list):
"""