@@ -40,7 +40,6 @@ def translate_polymorphic_filter_definitions_in_kwargs(
40
40
"""
41
41
additional_args = []
42
42
for field_path , val in kwargs .copy ().items (): # Python 3 needs copy
43
-
44
43
new_expr = _translate_polymorphic_filter_definition (
45
44
queryset_model , field_path , val , using = using
46
45
)
@@ -81,6 +80,29 @@ def tree_node_correct_field_specs(my_model, node):
81
80
return potential_q_object
82
81
83
82
83
+ def _deepcopy_q_object (q ):
84
+ """
85
+ Make a deepcopy of a Q-object.
86
+ """
87
+
88
+ def _copy_child (child ):
89
+ if isinstance (child , tuple ):
90
+ return child # tuples are immutable, no need to make a copy.
91
+ elif isinstance (child , Q ):
92
+ return _deepcopy_q_object (child )
93
+ else :
94
+ raise RuntimeError ("Unknown child type: %s" , type (child ))
95
+
96
+ children = [_copy_child (c ) for c in q .children ]
97
+
98
+ if hasattr (q , "copy" ): # Django 4.2+
99
+ obj = q .copy ()
100
+ obj .children = children
101
+ else :
102
+ obj = Q (* children , _connector = q .connector , _negated = q .negated )
103
+ return obj
104
+
105
+
84
106
def translate_polymorphic_filter_definitions_in_args (queryset_model , args , using = DEFAULT_DB_ALIAS ):
85
107
"""
86
108
Translate the non-keyword argument list for PolymorphicQuerySet.filter()
@@ -93,7 +115,8 @@ def translate_polymorphic_filter_definitions_in_args(queryset_model, args, using
93
115
Returns: modified Q objects
94
116
"""
95
117
return [
96
- translate_polymorphic_Q_object (queryset_model , copy .deepcopy (q ), using = using ) for q in args
118
+ translate_polymorphic_Q_object (queryset_model , _deepcopy_q_object (q ), using = using )
119
+ for q in args
97
120
]
98
121
99
122
0 commit comments