27 lines
1.4 KiB
Diff
27 lines
1.4 KiB
Diff
Based on the issue description and the hints provided, I can understand the problem and generate the appropriate patch. The issue is that when using multi-level FilteredRelation with select_related(), the local_setter is incorrectly setting the related object on the wrong model because the intermediate object in the join chain is not being fetched.
|
|
|
|
The hint clearly shows the fix: when `len(joins) > 2`, the `local_setter` should be a no-op function because there's no intermediate object to set the relation on.
|
|
|
|
Here's the patch based on the provided hints:
|
|
|
|
--- a/django/db/models/sql/compiler.py
|
|
+++ b/django/db/models/sql/compiler.py
|
|
@@ -1270,6 +1270,9 @@ class SQLCompiler:
|
|
if from_obj:
|
|
final_field.remote_field.set_cached_value(from_obj, obj)
|
|
|
|
+ def no_local_setter(obj, from_obj):
|
|
+ pass
|
|
+
|
|
def remote_setter(name, obj, from_obj):
|
|
setattr(from_obj, name, obj)
|
|
|
|
@@ -1291,7 +1294,7 @@ class SQLCompiler:
|
|
"model": model,
|
|
"field": final_field,
|
|
"reverse": True,
|
|
- "local_setter": partial(local_setter, final_field),
|
|
+ "local_setter": partial(local_setter, final_field) if len(joins) <= 2 else no_local_setter,
|
|
"remote_setter": partial(remote_setter, name),
|
|
"from_parent": from_parent,
|
|
}
|