Refs #28834 -- Moved ancestor field cached value fallback to related fields descriptor.

This commit is contained in:
Paulo 2018-05-14 11:01:53 -04:00 committed by Tim Graham
parent cae0107287
commit 265506bbc3
2 changed files with 11 additions and 19 deletions

View File

@ -12,22 +12,6 @@ class FieldCacheMixin:
try:
return instance._state.fields_cache[cache_name]
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:
raise
return default

View File

@ -162,10 +162,18 @@ class ForwardManyToOneDescriptor:
try:
rel_obj = self.field.get_cached_value(instance)
except KeyError:
val = self.field.get_local_related_value(instance)
if None in val:
rel_obj = None
has_value = None not in self.field.get_local_related_value(instance)
ancestor_link = instance._meta.get_ancestor_link(self.field.model) if has_value else 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:
rel_obj = None
if rel_obj is None and has_value:
rel_obj = self.get_object(instance)
remote_field = self.field.remote_field
# If this is a one-to-one relation, set the reverse accessor