Fixed #25546 -- Prevented duplicate queries with nested prefetch_related().

This commit is contained in:
François Freitag 2016-01-10 17:54:57 +01:00 committed by Tim Graham
parent 13023ba867
commit bdbe50a491
2 changed files with 81 additions and 0 deletions

View file

@ -1521,6 +1521,7 @@ def get_prefetcher(instance, attr):
rel_obj = getattr(instance, attr)
if hasattr(rel_obj, 'get_prefetch_queryset'):
prefetcher = rel_obj
is_fetched = attr in instance._prefetched_objects_cache
return prefetcher, rel_obj_descriptor, attr_found, is_fetched
@ -1597,6 +1598,7 @@ def prefetch_one_level(instances, prefetcher, lookup, level):
else:
if as_attr:
setattr(obj, to_attr, vals)
obj._prefetched_objects_cache[cache_name] = vals
else:
# Cache in the QuerySet.all().
qs = getattr(obj, to_attr).all()