Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
ca611958
Kaydet (Commit)
ca611958
authored
Eki 08, 2014
tarafından
Artem Rizhov
Kaydeden (comit)
Tim Graham
Eki 08, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #23555 -- Avoided suppressing IndexError in QuerySet.first() and .last()
üst
9e2e4cb6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
11 deletions
+48
-11
query.py
django/db/models/query.py
+8
-10
models.py
tests/get_earliest_or_latest/models.py
+15
-0
tests.py
tests/get_earliest_or_latest/tests.py
+25
-1
No files found.
django/db/models/query.py
Dosyayı görüntüle @
ca611958
...
...
@@ -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
):
"""
...
...
tests/get_earliest_or_latest/models.py
Dosyayı görüntüle @
ca611958
...
...
@@ -14,3 +14,18 @@ class Person(models.Model):
name
=
models
.
CharField
(
max_length
=
30
)
birthday
=
models
.
DateField
()
# Note that this model doesn't have "get_latest_by" set.
# Ticket #23555 - model with an intentionally broken QuerySet.__iter__ method.
class
IndexErrorQuerySet
(
models
.
QuerySet
):
"""
Emulates the case when some internal code raises an unexpected
IndexError.
"""
def
__iter__
(
self
):
raise
IndexError
class
IndexErrorArticle
(
Article
):
objects
=
IndexErrorQuerySet
.
as_manager
()
tests/get_earliest_or_latest/tests.py
Dosyayı görüntüle @
ca611958
...
...
@@ -4,7 +4,7 @@ from datetime import datetime
from
django.test
import
TestCase
from
.models
import
Article
,
Person
from
.models
import
Article
,
Person
,
IndexErrorArticle
class
EarliestOrLatestTests
(
TestCase
):
...
...
@@ -122,6 +122,9 @@ class EarliestOrLatestTests(TestCase):
self
.
assertRaises
(
AssertionError
,
Person
.
objects
.
latest
)
self
.
assertEqual
(
Person
.
objects
.
latest
(
"birthday"
),
p2
)
class
TestFirstLast
(
TestCase
):
def
test_first
(
self
):
p1
=
Person
.
objects
.
create
(
name
=
"Bob"
,
birthday
=
datetime
(
1950
,
1
,
1
))
p2
=
Person
.
objects
.
create
(
name
=
"Alice"
,
birthday
=
datetime
(
1961
,
2
,
3
))
...
...
@@ -152,3 +155,24 @@ class EarliestOrLatestTests(TestCase):
self
.
assertIs
(
Person
.
objects
.
filter
(
birthday__lte
=
datetime
(
1940
,
1
,
1
))
.
last
(),
None
)
def
test_index_error_not_suppressed
(
self
):
"""
#23555 -- Unexpected IndexError exceptions in QuerySet iteration
shouldn't be suppressed.
"""
def
check
():
# We know that we've broken the __iter__ method, so the queryset
# should always raise an exception.
self
.
assertRaises
(
IndexError
,
lambda
:
IndexErrorArticle
.
objects
.
all
()[
0
])
self
.
assertRaises
(
IndexError
,
IndexErrorArticle
.
objects
.
all
()
.
first
)
self
.
assertRaises
(
IndexError
,
IndexErrorArticle
.
objects
.
all
()
.
last
)
check
()
# And it does not matter if there are any records in the DB.
IndexErrorArticle
.
objects
.
create
(
headline
=
"Article 1"
,
pub_date
=
datetime
(
2005
,
7
,
26
),
expire_date
=
datetime
(
2005
,
9
,
1
)
)
check
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment