Refs #28834 -- Moved ancestor field cached value fallback to related fields descriptor.
This commit is contained in:
parent
cae0107287
commit
265506bbc3
|
@ -12,22 +12,6 @@ class FieldCacheMixin:
|
||||||
try:
|
try:
|
||||||
return instance._state.fields_cache[cache_name]
|
return instance._state.fields_cache[cache_name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# An ancestor link will exist if this field is defined on a
|
|
||||||
# multi-table inheritance parent of the instance's class.
|
|
||||||
ancestor_link = instance._meta.get_ancestor_link(self.model)
|
|
||||||
if ancestor_link:
|
|
||||||
try:
|
|
||||||
# The value might be cached on an ancestor if the instance
|
|
||||||
# originated from walking down the inheritance chain.
|
|
||||||
ancestor = ancestor_link.get_cached_value(instance)
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
value = self.get_cached_value(ancestor)
|
|
||||||
# Cache the ancestor value locally to speed up future
|
|
||||||
# lookups.
|
|
||||||
self.set_cached_value(instance, value)
|
|
||||||
return value
|
|
||||||
if default is NOT_PROVIDED:
|
if default is NOT_PROVIDED:
|
||||||
raise
|
raise
|
||||||
return default
|
return default
|
||||||
|
|
|
@ -162,10 +162,18 @@ class ForwardManyToOneDescriptor:
|
||||||
try:
|
try:
|
||||||
rel_obj = self.field.get_cached_value(instance)
|
rel_obj = self.field.get_cached_value(instance)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
val = self.field.get_local_related_value(instance)
|
has_value = None not in self.field.get_local_related_value(instance)
|
||||||
if None in val:
|
ancestor_link = instance._meta.get_ancestor_link(self.field.model) if has_value else None
|
||||||
rel_obj = None
|
if ancestor_link and ancestor_link.is_cached(instance):
|
||||||
|
# An ancestor link will exist if this field is defined on a
|
||||||
|
# multi-table inheritance parent of the instance's class.
|
||||||
|
ancestor = ancestor_link.get_cached_value(instance)
|
||||||
|
# The value might be cached on an ancestor if the instance
|
||||||
|
# originated from walking down the inheritance chain.
|
||||||
|
rel_obj = self.field.get_cached_value(ancestor, default=None)
|
||||||
else:
|
else:
|
||||||
|
rel_obj = None
|
||||||
|
if rel_obj is None and has_value:
|
||||||
rel_obj = self.get_object(instance)
|
rel_obj = self.get_object(instance)
|
||||||
remote_field = self.field.remote_field
|
remote_field = self.field.remote_field
|
||||||
# If this is a one-to-one relation, set the reverse accessor
|
# If this is a one-to-one relation, set the reverse accessor
|
||||||
|
|
Loading…
Reference in New Issue