Skip to content

Commit 6616348

Browse files
committed
Replace a deepcopy of the Q object custom deep copy
1 parent bd5faf0 commit 6616348

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

polymorphic/query_translate.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ def tree_node_correct_field_specs(my_model, node):
8181
return potential_q_object
8282

8383

84+
def _deepcopy_q_object(q):
85+
"""
86+
Make a deepcopy of a Q-object.
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+
obj = q.copy() # Use built in shallow copy from Q-object
97+
obj.children = [_copy_child(c) for c in obj.children] # replace children
98+
return obj
99+
100+
84101
def translate_polymorphic_filter_definitions_in_args(queryset_model, args, using=DEFAULT_DB_ALIAS):
85102
"""
86103
Translate the non-keyword argument list for PolymorphicQuerySet.filter()
@@ -93,7 +110,7 @@ def translate_polymorphic_filter_definitions_in_args(queryset_model, args, using
93110
Returns: modified Q objects
94111
"""
95112
return [
96-
translate_polymorphic_Q_object(queryset_model, copy.deepcopy(q), using=using) for q in args
113+
translate_polymorphic_Q_object(queryset_model, _deepcopy_q_object(q), using=using) for q in args
97114
]
98115

99116

0 commit comments

Comments
 (0)