summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorTai Lee <tai.lee@3030.com.au>2012-11-28 18:16:00 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2012-11-28 18:17:10 +0200
commit6ebf115206289bce8f3d86318871faac13d6e835 (patch)
treeab51d6aff68672c4d8b6eb601f5c40c5c76e4413 /django/db/models/sql/query.py
parent2a0e4c249f30ef591c8b1ca36e9e1f38bf3395b4 (diff)
Fixed #14694 -- Made ``defer()`` work with reverse relations
Reverse o2o fields are now usable with defer.
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py20
1 files changed, 16 insertions, 4 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 9c2e140225..e24dc22ee7 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -599,17 +599,22 @@ class Query(object):
for name in parts[:-1]:
old_model = cur_model
source = opts.get_field_by_name(name)[0]
- cur_model = source.rel.to
+ if is_reverse_o2o(source):
+ cur_model = source.model
+ else:
+ cur_model = source.rel.to
opts = cur_model._meta
# Even if we're "just passing through" this model, we must add
# both the current model's pk and the related reference field
- # to the things we select.
- must_include[old_model].add(source)
+ # (if it's not a reverse relation) to the things we select.
+ if not is_reverse_o2o(source):
+ must_include[old_model].add(source)
add_to_dict(must_include, cur_model, opts.pk)
field, model, _, _ = opts.get_field_by_name(parts[-1])
if model is None:
model = cur_model
- add_to_dict(seen, model, field)
+ if not is_reverse_o2o(field):
+ add_to_dict(seen, model, field)
if defer:
# We need to load all fields for each model, except those that
@@ -1983,3 +1988,10 @@ def add_to_dict(data, key, value):
data[key].add(value)
else:
data[key] = set([value])
+
+def is_reverse_o2o(field):
+ """
+ A little helper to check if the given field is reverse-o2o. The field is
+ expected to be some sort of relation field or related object.
+ """
+ return not hasattr(field, 'rel') and field.field.unique