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
eee34ef6
Kaydet (Commit)
eee34ef6
authored
May 17, 2014
tarafından
Matthias Erll
Kaydeden (comit)
Tim Graham
May 31, 2017
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #22550 -- Prohibited QuerySet.last()/reverse() after slicing.
üst
84fb50df
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
26 additions
and
1 deletion
+26
-1
query.py
django/db/models/query.py
+2
-0
2.0.txt
docs/releases/2.0.txt
+12
-0
queries.txt
docs/topics/db/queries.txt
+3
-0
tests.py
tests/ordering/tests.py
+8
-0
tests.py
tests/queries/tests.py
+1
-1
No files found.
django/db/models/query.py
Dosyayı görüntüle @
eee34ef6
...
@@ -944,6 +944,8 @@ class QuerySet:
...
@@ -944,6 +944,8 @@ class QuerySet:
def
reverse
(
self
):
def
reverse
(
self
):
"""Reverse the ordering of the QuerySet."""
"""Reverse the ordering of the QuerySet."""
if
not
self
.
query
.
can_filter
():
raise
TypeError
(
'Cannot reverse a query once a slice has been taken.'
)
clone
=
self
.
_clone
()
clone
=
self
.
_clone
()
clone
.
query
.
standard_ordering
=
not
clone
.
query
.
standard_ordering
clone
.
query
.
standard_ordering
=
not
clone
.
query
.
standard_ordering
return
clone
return
clone
...
...
docs/releases/2.0.txt
Dosyayı görüntüle @
eee34ef6
...
@@ -314,6 +314,18 @@ If you wish to keep this restriction in the admin when editing users, set
...
@@ -314,6 +314,18 @@ If you wish to keep this restriction in the admin when editing users, set
admin.site.unregister(User)
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
admin.site.register(User, MyUserAdmin)
``QuerySet.reverse()`` and ``last()`` are prohibited after slicing
------------------------------------------------------------------
Calling ``QuerySet.reverse()`` or ``last()`` on a sliced queryset leads to
unexpected results due to the slice being applied after reordering. This is
now prohibited, e.g.::
>>> Model.objects.all()[:2].reverse()
Traceback (most recent call last):
...
TypeError: Cannot reverse a query once a slice has been taken.
Miscellaneous
Miscellaneous
-------------
-------------
...
...
docs/topics/db/queries.txt
Dosyayı görüntüle @
eee34ef6
...
@@ -361,6 +361,9 @@ every *second* object of the first 10::
...
@@ -361,6 +361,9 @@ every *second* object of the first 10::
>>> Entry.objects.all()[:10:2]
>>> Entry.objects.all()[:10:2]
Further filtering or ordering of a sliced queryset is prohibited due to the
ambiguous nature of how that might work.
To retrieve a *single* object rather than a list
To retrieve a *single* object rather than a list
(e.g. ``SELECT foo FROM bar LIMIT 1``), use a simple index instead of a
(e.g. ``SELECT foo FROM bar LIMIT 1``), use a simple index instead of a
slice. For example, this returns the first ``Entry`` in the database, after
slice. For example, this returns the first ``Entry`` in the database, after
...
...
tests/ordering/tests.py
Dosyayı görüntüle @
eee34ef6
...
@@ -203,6 +203,14 @@ class OrderingTests(TestCase):
...
@@ -203,6 +203,14 @@ class OrderingTests(TestCase):
attrgetter
(
"headline"
)
attrgetter
(
"headline"
)
)
)
def
test_no_reordering_after_slicing
(
self
):
msg
=
'Cannot reverse a query once a slice has been taken.'
qs
=
Article
.
objects
.
all
()[
0
:
2
]
with
self
.
assertRaisesMessage
(
TypeError
,
msg
):
qs
.
reverse
()
with
self
.
assertRaisesMessage
(
TypeError
,
msg
):
qs
.
last
()
def
test_extra_ordering
(
self
):
def
test_extra_ordering
(
self
):
"""
"""
Ordering can be based on fields included from an 'extra' clause
Ordering can be based on fields included from an 'extra' clause
...
...
tests/queries/tests.py
Dosyayı görüntüle @
eee34ef6
...
@@ -731,10 +731,10 @@ class Queries1Tests(TestCase):
...
@@ -731,10 +731,10 @@ class Queries1Tests(TestCase):
q
.
extra
(
select
=
{
'foo'
:
"1"
}),
q
.
extra
(
select
=
{
'foo'
:
"1"
}),
[]
[]
)
)
self
.
assertQuerysetEqual
(
q
.
reverse
(),
[])
q
.
query
.
low_mark
=
1
q
.
query
.
low_mark
=
1
with
self
.
assertRaisesMessage
(
AssertionError
,
'Cannot change a query once a slice has been taken'
):
with
self
.
assertRaisesMessage
(
AssertionError
,
'Cannot change a query once a slice has been taken'
):
q
.
extra
(
select
=
{
'foo'
:
"1"
})
q
.
extra
(
select
=
{
'foo'
:
"1"
})
self
.
assertQuerysetEqual
(
q
.
reverse
(),
[])
self
.
assertQuerysetEqual
(
q
.
defer
(
'meal'
),
[])
self
.
assertQuerysetEqual
(
q
.
defer
(
'meal'
),
[])
self
.
assertQuerysetEqual
(
q
.
only
(
'meal'
),
[])
self
.
assertQuerysetEqual
(
q
.
only
(
'meal'
),
[])
...
...
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