MCPcopy
hub / github.com/django/django / clone

Method clone

django/db/models/sql/query.py:387–430  ·  view source on GitHub ↗

Return a copy of the current Query. A lightweight alternative to deepcopy().

(self)

Source from the content-addressed store, hash-verified

385 return self.model._meta
386
387 def clone(self):
388 """
389 Return a copy of the current Query. A lightweight alternative to
390 deepcopy().
391 """
392 obj = Empty()
393 obj.__class__ = self.__class__
394 # Copy references to everything.
395 obj.__dict__ = self.__dict__.copy()
396 # Clone attributes that can't use shallow copy.
397 obj.alias_refcount = self.alias_refcount.copy()
398 obj.alias_map = self.alias_map.copy()
399 obj.external_aliases = self.external_aliases.copy()
400 obj.table_map = self.table_map.copy()
401 obj.where = self.where.clone()
402 obj.annotations = self.annotations.copy()
403 if self.annotation_select_mask is not None:
404 obj.annotation_select_mask = self.annotation_select_mask.copy()
405 if self.combined_queries:
406 obj.combined_queries = tuple(
407 [query.clone() for query in self.combined_queries]
408 )
409 # _annotation_select_cache cannot be copied, as doing so breaks the
410 # (necessary) state in which both annotations and
411 # _annotation_select_cache point to the same underlying objects.
412 # It will get re-populated in the cloned queryset the next time it's
413 # used.
414 obj._annotation_select_cache = None
415 obj.extra = self.extra.copy()
416 if self.extra_select_mask is not None:
417 obj.extra_select_mask = self.extra_select_mask.copy()
418 if self._extra_select_cache is not None:
419 obj._extra_select_cache = self._extra_select_cache.copy()
420 if self.select_related is not False:
421 # Use deepcopy because select_related stores fields in nested
422 # dicts.
423 obj.select_related = copy.deepcopy(obj.select_related)
424 if "subq_aliases" in self.__dict__:
425 obj.subq_aliases = self.subq_aliases.copy()
426 obj.used_aliases = self.used_aliases.copy()
427 obj._filtered_relations = self._filtered_relations.copy()
428 # Clear the cached_property, if it exists.
429 obj.__dict__.pop("base_table", None)
430 return obj
431
432 def chain(self, klass=None):
433 """

Callers 15

__deepcopy__Method · 0.95
chainMethod · 0.95
relabeled_cloneMethod · 0.95
get_aggregationMethod · 0.95
get_countMethod · 0.95
existsMethod · 0.95
explainMethod · 0.95
resolve_expressionMethod · 0.95
get_qualify_sqlMethod · 0.45

Calls 3

EmptyClass · 0.90
copyMethod · 0.45
popMethod · 0.45

Tested by 1