diff options
| author | Tai Lee <tai.lee@3030.com.au> | 2012-11-28 18:16:00 +0200 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2012-11-28 18:25:34 +0200 |
| commit | a21e8dee7682abdba646c5b72b6371d392e44dd1 (patch) | |
| tree | 22006706ea1a5edd53d11d888cd1a51b9db2c102 /django/db/models/sql | |
| parent | 54ce0ff7ba2afbac6d0a18e282b336e0ddb8c3a6 (diff) | |
[1.5.x] Fixed #14694 -- Made ``defer()`` work with reverse relations
Reverse o2o fields are now usable with defer.
Backpatch of [6ebf115206289bce8f3d86318871faac13d6e835]
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 0d79230391..9c2af10e35 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -585,17 +585,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 @@ -2032,3 +2037,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 |
