diff options
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/query.py | 20 |
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 |
