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
b9f8635f
Kaydet (Commit)
b9f8635f
authored
May 01, 2016
tarafından
Michal Petrucha
Kaydeden (comit)
Tim Graham
May 03, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Refs #16508 -- Added invalidation of stale cached instances of GenericForeignKey targets.
üst
7ec330ee
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
15 deletions
+41
-15
fields.py
django/contrib/contenttypes/fields.py
+24
-15
tests.py
tests/generic_relations/tests.py
+17
-0
No files found.
django/contrib/contenttypes/fields.py
Dosyayı görüntüle @
b9f8635f
...
@@ -208,7 +208,7 @@ class GenericForeignKey(object):
...
@@ -208,7 +208,7 @@ class GenericForeignKey(object):
lambda
obj
:
(
obj
.
_get_pk_val
(),
obj
.
__class__
),
lambda
obj
:
(
obj
.
_get_pk_val
(),
obj
.
__class__
),
gfk_key
,
gfk_key
,
True
,
True
,
self
.
cache_attr
)
self
.
name
)
def
is_cached
(
self
,
instance
):
def
is_cached
(
self
,
instance
):
return
hasattr
(
instance
,
self
.
cache_attr
)
return
hasattr
(
instance
,
self
.
cache_attr
)
...
@@ -217,26 +217,35 @@ class GenericForeignKey(object):
...
@@ -217,26 +217,35 @@ class GenericForeignKey(object):
if
instance
is
None
:
if
instance
is
None
:
return
self
return
self
# Don't use getattr(instance, self.ct_field) here because that might
# reload the same ContentType over and over (#5570). Instead, get the
# content type ID here, and later when the actual instance is needed,
# use ContentType.objects.get_for_id(), which has a global cache.
f
=
self
.
model
.
_meta
.
get_field
(
self
.
ct_field
)
ct_id
=
getattr
(
instance
,
f
.
get_attname
(),
None
)
pk_val
=
getattr
(
instance
,
self
.
fk_field
)
try
:
try
:
re
turn
getattr
(
instance
,
self
.
cache_attr
)
re
l_obj
=
getattr
(
instance
,
self
.
cache_attr
)
except
AttributeError
:
except
AttributeError
:
rel_obj
=
None
rel_obj
=
None
else
:
if
rel_obj
and
(
ct_id
!=
self
.
get_content_type
(
obj
=
rel_obj
,
using
=
instance
.
_state
.
db
)
.
id
or
rel_obj
.
_meta
.
pk
.
to_python
(
pk_val
)
!=
rel_obj
.
_get_pk_val
()):
rel_obj
=
None
# Make sure to use ContentType.objects.get_for_id() to ensure that
if
rel_obj
is
not
None
:
# lookups are cached (see ticket #5570). This takes more code than
# the naive ``getattr(instance, self.ct_field)``, but has better
# performance when dealing with GFKs in loops and such.
f
=
self
.
model
.
_meta
.
get_field
(
self
.
ct_field
)
ct_id
=
getattr
(
instance
,
f
.
get_attname
(),
None
)
if
ct_id
is
not
None
:
ct
=
self
.
get_content_type
(
id
=
ct_id
,
using
=
instance
.
_state
.
db
)
try
:
rel_obj
=
ct
.
get_object_for_this_type
(
pk
=
getattr
(
instance
,
self
.
fk_field
))
except
ObjectDoesNotExist
:
pass
setattr
(
instance
,
self
.
cache_attr
,
rel_obj
)
return
rel_obj
return
rel_obj
if
ct_id
is
not
None
:
ct
=
self
.
get_content_type
(
id
=
ct_id
,
using
=
instance
.
_state
.
db
)
try
:
rel_obj
=
ct
.
get_object_for_this_type
(
pk
=
pk_val
)
except
ObjectDoesNotExist
:
pass
setattr
(
instance
,
self
.
cache_attr
,
rel_obj
)
return
rel_obj
def
__set__
(
self
,
instance
,
value
):
def
__set__
(
self
,
instance
,
value
):
ct
=
None
ct
=
None
fk
=
None
fk
=
None
...
...
tests/generic_relations/tests.py
Dosyayı görüntüle @
b9f8635f
...
@@ -555,6 +555,23 @@ id="id_generic_relations-taggeditem-content_type-object_id-1-id" /></p>""" % tag
...
@@ -555,6 +555,23 @@ id="id_generic_relations-taggeditem-content_type-object_id-1-id" /></p>""" % tag
with
self
.
assertRaises
(
IntegrityError
):
with
self
.
assertRaises
(
IntegrityError
):
TaggedItem
.
objects
.
create
(
tag
=
"shiny"
,
content_object
=
quartz
)
TaggedItem
.
objects
.
create
(
tag
=
"shiny"
,
content_object
=
quartz
)
def
test_cache_invalidation_for_content_type_id
(
self
):
# Create a Vegetable and Mineral with the same id.
new_id
=
max
(
Vegetable
.
objects
.
order_by
(
'-id'
)[
0
]
.
id
,
Mineral
.
objects
.
order_by
(
'-id'
)[
0
]
.
id
)
+
1
broccoli
=
Vegetable
.
objects
.
create
(
id
=
new_id
,
name
=
"Broccoli"
)
diamond
=
Mineral
.
objects
.
create
(
id
=
new_id
,
name
=
"Diamond"
,
hardness
=
7
)
tag
=
TaggedItem
.
objects
.
create
(
content_object
=
broccoli
,
tag
=
"yummy"
)
tag
.
content_type
=
ContentType
.
objects
.
get_for_model
(
diamond
)
self
.
assertEqual
(
tag
.
content_object
,
diamond
)
def
test_cache_invalidation_for_object_id
(
self
):
broccoli
=
Vegetable
.
objects
.
create
(
name
=
"Broccoli"
)
cauliflower
=
Vegetable
.
objects
.
create
(
name
=
"Cauliflower"
)
tag
=
TaggedItem
.
objects
.
create
(
content_object
=
broccoli
,
tag
=
"yummy"
)
tag
.
object_id
=
cauliflower
.
id
self
.
assertEqual
(
tag
.
content_object
,
cauliflower
)
class
CustomWidget
(
forms
.
TextInput
):
class
CustomWidget
(
forms
.
TextInput
):
pass
pass
...
...
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