From e1ae2b00504ba30481285b2bd767d1ad561bf4be Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Fri, 11 Oct 2019 07:56:02 +0200 Subject: [PATCH] Refs #30854 -- Moved local_setter() outside the loop in SQLCompiler.get_related_selections(). --- django/db/models/sql/compiler.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 85adbfdcd98..f475b82921c 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -890,6 +890,12 @@ class SQLCompiler: select, model._meta, alias, cur_depth + 1, next, restricted) get_related_klass_infos(klass_info, next_klass_infos) + + def local_setter(obj, from_obj): + # Set a reverse fk object when relation is non-empty. + if from_obj: + f.remote_field.set_cached_value(from_obj, obj) + for name in list(requested): # Filtered relations work only on the topmost level. if cur_depth > 1: @@ -901,11 +907,6 @@ class SQLCompiler: alias = joins[-1] from_parent = issubclass(model, opts.model) and model is not opts.model - def local_setter(obj, from_obj): - # Set a reverse fk object when relation is non-empty. - if from_obj: - f.remote_field.set_cached_value(from_obj, obj) - def remote_setter(obj, from_obj): setattr(from_obj, name, obj) klass_info = {