mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
[1.5.x] Fixed #14694 -- Made `defer()` work with reverse relations
Reverse o2o fields are now usable with defer.
Backpatch of [6ebf115206]
This commit is contained in:
committed by
Anssi Kääriäinen
parent
54ce0ff7ba
commit
a21e8dee76
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user