MCPcopy
hub / github.com/django/django / trim_joins

Method trim_joins

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

The 'target' parameter is the final field being joined to, 'joins' is the full list of join aliases. The 'path' contain the PathInfos used to create the joins. Return the final target field and table alias and the new active joins. Always trim any d

(self, targets, joins, path)

Source from the content-addressed store, hash-verified

2005 return JoinInfo(final_field, targets, opts, joins, path, final_transformer)
2006
2007 def trim_joins(self, targets, joins, path):
2008 """
2009 The 'target' parameter is the final field being joined to, 'joins'
2010 is the full list of join aliases. The 'path' contain the PathInfos
2011 used to create the joins.
2012
2013 Return the final target field and table alias and the new active
2014 joins.
2015
2016 Always trim any direct join if the target column is already in the
2017 previous table. Can't trim reverse joins as it's unknown if there's
2018 anything on the other side of the join.
2019 """
2020 joins = joins[:]
2021 for pos, info in enumerate(reversed(path)):
2022 if len(joins) == 1 or not info.direct:
2023 break
2024 if info.filtered_relation:
2025 break
2026 join_targets = {t.column for t in info.join_field.foreign_related_fields}
2027 cur_targets = {t.column for t in targets}
2028 if not cur_targets.issubset(join_targets):
2029 break
2030 targets_dict = {
2031 r[1].column: r[0]
2032 for r in info.join_field.related_fields
2033 if r[1].column in cur_targets
2034 }
2035 targets = tuple(targets_dict[t.column] for t in targets)
2036 self.unref_alias(joins.pop())
2037 return targets, joins[-1], joins
2038
2039 @classmethod
2040 def _gen_cols(cls, exprs, include_external=False, resolve_refs=True):

Callers 5

build_filterMethod · 0.95
resolve_refMethod · 0.95
add_fieldsMethod · 0.95
get_distinctMethod · 0.80
find_ordering_nameMethod · 0.80

Calls 2

unref_aliasMethod · 0.95
popMethod · 0.45

Tested by

no test coverage detected