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:
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user