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
c0a2b950
Kaydet (Commit)
c0a2b950
authored
Mar 04, 2017
tarafından
François Freitag
Kaydeden (comit)
Tim Graham
May 04, 2017
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #27554 -- Fixed prefetch_related() crash when fetching relations in nested Prefetches.
üst
16121da7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
4 deletions
+46
-4
query.py
django/db/models/query.py
+9
-4
1.11.1.txt
docs/releases/1.11.1.txt
+3
-0
tests.py
tests/prefetch_related/tests.py
+34
-0
No files found.
django/db/models/query.py
Dosyayı görüntüle @
c0a2b950
...
...
@@ -1423,10 +1423,15 @@ def prefetch_related_objects(model_instances, *related_lookups):
# that we can continue with nullable or reverse relations.
new_obj_list
=
[]
for
obj
in
obj_list
:
try
:
new_obj
=
getattr
(
obj
,
through_attr
)
except
exceptions
.
ObjectDoesNotExist
:
continue
if
through_attr
in
getattr
(
obj
,
'_prefetched_objects_cache'
,
()):
# If related objects have been prefetched, use the
# cache rather than the object's through_attr.
new_obj
=
list
(
obj
.
_prefetched_objects_cache
.
get
(
through_attr
))
else
:
try
:
new_obj
=
getattr
(
obj
,
through_attr
)
except
exceptions
.
ObjectDoesNotExist
:
continue
if
new_obj
is
None
:
continue
# We special-case `list` rather than something more generic
...
...
docs/releases/1.11.1.txt
Dosyayı görüntüle @
c0a2b950
...
...
@@ -91,3 +91,6 @@ Bugfixes
* Corrected the return type of ``ArrayField(CITextField())`` values retrieved
from the database (:ticket:`28161`).
* Fixed ``QuerySet.prefetch_related()`` crash when fetching relations in nested
``Prefetch`` objects (:ticket:`27554`).
tests/prefetch_related/tests.py
Dosyayı görüntüle @
c0a2b950
...
...
@@ -1361,3 +1361,37 @@ class Ticket25546Tests(TestCase):
self
.
assertEqual
(
book1
.
first_authors
[
0
]
.
happy_place
,
[
self
.
author1_address1
])
self
.
assertEqual
(
book1
.
first_authors
[
1
]
.
happy_place
,
[])
self
.
assertEqual
(
book2
.
first_authors
[
0
]
.
happy_place
,
[
self
.
author2_address1
])
class
ReadPrefetchedObjectsCacheTests
(
TestCase
):
@classmethod
def
setUpTestData
(
cls
):
cls
.
book1
=
Book
.
objects
.
create
(
title
=
'Les confessions Volume I'
)
cls
.
book2
=
Book
.
objects
.
create
(
title
=
'Candide'
)
cls
.
author1
=
AuthorWithAge
.
objects
.
create
(
name
=
'Rousseau'
,
first_book
=
cls
.
book1
,
age
=
70
)
cls
.
author2
=
AuthorWithAge
.
objects
.
create
(
name
=
'Voltaire'
,
first_book
=
cls
.
book2
,
age
=
65
)
cls
.
book1
.
authors
.
add
(
cls
.
author1
)
cls
.
book2
.
authors
.
add
(
cls
.
author2
)
FavoriteAuthors
.
objects
.
create
(
author
=
cls
.
author1
,
likes_author
=
cls
.
author2
)
def
test_retrieves_results_from_prefetched_objects_cache
(
self
):
"""
When intermediary results are prefetched without a destination
attribute, they are saved in the RelatedManager's cache
(_prefetched_objects_cache). prefetch_related() uses this cache
(#27554).
"""
authors
=
AuthorWithAge
.
objects
.
prefetch_related
(
Prefetch
(
'author'
,
queryset
=
Author
.
objects
.
prefetch_related
(
# Results are saved in the RelatedManager's cache
# (_prefetched_objects_cache) and do not replace the
# RelatedManager on Author instances (favorite_authors)
Prefetch
(
'favorite_authors__first_book'
),
),
),
)
with
self
.
assertNumQueries
(
4
):
# AuthorWithAge -> Author -> FavoriteAuthors, Book
self
.
assertQuerysetEqual
(
authors
,
[
'<AuthorWithAge: Rousseau>'
,
'<AuthorWithAge: Voltaire>'
])
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