summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2015-11-11 00:40:20 -0500
committerSimon Charette <charette.s@gmail.com>2015-11-11 12:25:10 -0500
commit4a9c32f5eece9030c2b568e930cec0c1ba8f1da0 (patch)
treef1548f789e468b0c8791494cf4d1739e1cd4059e
parent4cd5d846d4a0a62bba6edf435a8ae9c6dcfacb43 (diff)
Refs #25693 -- Avoided redundant calls to get_fields() in `to_attr` validation.
-rw-r--r--django/db/models/query.py25
1 files changed, 14 insertions, 11 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 1bf890e9eb..006feb6427 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -1571,20 +1571,23 @@ def prefetch_one_level(instances, prefetcher, lookup, level):
rel_attr_val = rel_obj_attr(rel_obj)
rel_obj_cache.setdefault(rel_attr_val, []).append(rel_obj)
+ to_attr, as_attr = lookup.get_current_to_attr(level)
+ # Make sure `to_attr` does not conflict with a field.
+ if as_attr and instances:
+ # We assume that objects retrieved are homogeneous (which is the premise
+ # of prefetch_related), so what applies to first object applies to all.
+ model = instances[0].__class__
+ try:
+ model._meta.get_field(to_attr)
+ except exceptions.FieldDoesNotExist:
+ pass
+ else:
+ msg = 'to_attr={} conflicts with a field on the {} model.'
+ raise ValueError(msg.format(to_attr, model.__name__))
+
for obj in instances:
instance_attr_val = instance_attr(obj)
vals = rel_obj_cache.get(instance_attr_val, [])
- to_attr, as_attr = lookup.get_current_to_attr(level)
-
- # Check we are not shadowing a field on obj.
- if as_attr:
- try:
- field = obj._meta.get_field(to_attr)
- except exceptions.FieldDoesNotExist:
- pass
- else:
- msg = 'to_attr={} conflicts with a field on the {} model.'
- raise ValueError(msg.format(to_attr, field.model.__name__))
if single:
val = vals[0] if vals else None