diff options
| author | Simon Charette <charette.s@gmail.com> | 2016-07-19 14:55:59 -0400 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2016-07-19 16:05:07 -0400 |
| commit | 271bfe65d986f5ecbaeb7a70a3092356c0a9e222 (patch) | |
| tree | a2e4ce4350b07d1261a96bd0a3a92e8a023f45a5 /django | |
| parent | 081fdaf110386db940d834ba51d93e23aa293fcd (diff) | |
Fixed #26916 -- Fixed prefetch_related when using a cached_property as to_attr.
Thanks Trac alias karyon for the report and Tim for the review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/query.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index cff7135ef6..05f6ed370d 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -25,7 +25,7 @@ from django.db.models.query_utils import ( from django.db.models.sql.constants import CURSOR from django.utils import six, timezone from django.utils.deprecation import RemovedInDjango20Warning -from django.utils.functional import partition +from django.utils.functional import cached_property, partition from django.utils.version import get_version # The maximum number of items to display in a QuerySet.__repr__ @@ -1545,7 +1545,12 @@ def get_prefetcher(instance, through_attr, to_attr): if hasattr(rel_obj, 'get_prefetch_queryset'): prefetcher = rel_obj if through_attr != to_attr: - is_fetched = hasattr(instance, to_attr) + # Special case cached_property instances because hasattr + # triggers attribute computation and assignment. + if isinstance(getattr(instance.__class__, to_attr, None), cached_property): + is_fetched = to_attr in instance.__dict__ + else: + is_fetched = hasattr(instance, to_attr) else: is_fetched = through_attr in instance._prefetched_objects_cache return prefetcher, rel_obj_descriptor, attr_found, is_fetched |
