mirror of
https://github.com/django/django.git
synced 2025-09-26 12:09:19 +00:00
Fixed #17485 -- Made defer work with select_related
This commit tackles a couple of issues. First, in certain cases there were some mixups if field.attname or field.name should be deferred. Field.attname is now always used. Another issue tackled is a case where field is both deferred by .only(), and selected by select_related. This case is now an error. A lot of thanks to koniiiik (Michal Petrucha) for the patch, and to Andrei Antoukh for review.
This commit is contained in:
parent
5318783027
commit
b6c356b7bb
9 changed files with 81 additions and 21 deletions
|
@ -1296,7 +1296,7 @@ def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None,
|
|||
# Build the list of fields that *haven't* been requested
|
||||
for field, model in klass._meta.get_fields_with_model():
|
||||
if field.name not in load_fields:
|
||||
skip.add(field.name)
|
||||
skip.add(field.attname)
|
||||
elif local_only and model is not None:
|
||||
continue
|
||||
else:
|
||||
|
@ -1327,7 +1327,7 @@ def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None,
|
|||
|
||||
related_fields = []
|
||||
for f in klass._meta.fields:
|
||||
if select_related_descend(f, restricted, requested):
|
||||
if select_related_descend(f, restricted, requested, load_fields):
|
||||
if restricted:
|
||||
next = requested[f.name]
|
||||
else:
|
||||
|
@ -1339,7 +1339,8 @@ def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None,
|
|||
reverse_related_fields = []
|
||||
if restricted:
|
||||
for o in klass._meta.get_all_related_objects():
|
||||
if o.field.unique and select_related_descend(o.field, restricted, requested, reverse=True):
|
||||
if o.field.unique and select_related_descend(o.field, restricted, requested,
|
||||
only_load.get(o.model), reverse=True):
|
||||
next = requested[o.field.related_query_name()]
|
||||
klass_info = get_klass_info(o.model, max_depth=max_depth, cur_depth=cur_depth+1,
|
||||
requested=next, only_load=only_load, local_only=True)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue