MCPcopy
hub / github.com/django/django / resolve_ref

Method resolve_ref

django/db/models/sql/query.py:2061–2110  ·  view source on GitHub ↗
(self, name, allow_joins=True, reuse=None, summarize=False)

Source from the content-addressed store, hash-verified

2059 yield from (expr.alias for expr in cls._gen_cols(exprs))
2060
2061 def resolve_ref(self, name, allow_joins=True, reuse=None, summarize=False):
2062 annotation = self.annotations.get(name)
2063 if annotation is not None:
2064 if not allow_joins:
2065 for alias in self._gen_col_aliases([annotation]):
2066 if isinstance(self.alias_map[alias], Join):
2067 raise FieldError(
2068 "Joined field references are not permitted in this query"
2069 )
2070 if summarize:
2071 # Summarize currently means we are doing an aggregate() query
2072 # which is executed as a wrapped subquery if any of the
2073 # aggregate() elements reference an existing annotation. In
2074 # that case we need to return a Ref to the subquery's
2075 # annotation.
2076 if name not in self.annotation_select:
2077 raise FieldError(
2078 "Cannot aggregate over the '%s' alias. Use annotate() "
2079 "to promote it." % name
2080 )
2081 return Ref(name, self.annotation_select[name])
2082 else:
2083 return annotation
2084 else:
2085 field_list = name.split(LOOKUP_SEP)
2086 annotation = self.annotations.get(field_list[0])
2087 if annotation is not None:
2088 for transform in field_list[1:]:
2089 annotation = self.try_transform(annotation, transform)
2090 return annotation
2091 join_info = self.setup_joins(
2092 field_list, self.get_meta(), self.get_initial_alias(), can_reuse=reuse
2093 )
2094 targets, final_alias, join_list = self.trim_joins(
2095 join_info.targets, join_info.joins, join_info.path
2096 )
2097 if not allow_joins and len(join_list) > 1:
2098 raise FieldError(
2099 "Joined field references are not permitted in this query"
2100 )
2101 if len(targets) > 1:
2102 raise FieldError(
2103 "Referencing multicolumn fields with F() objects isn't supported"
2104 )
2105 # Verify that the last lookup in name is a field or a transform:
2106 # transform_function() raises FieldError if not.
2107 transform = join_info.transform_function(targets[0], final_alias)
2108 if reuse is not None:
2109 reuse.update(join_list)
2110 return transform
2111
2112 def split_exclude(self, filter_expr, can_reuse, names_with_path):
2113 """

Callers 5

resolve_expressionMethod · 0.80
resolve_expressionMethod · 0.80
resolve_expressionMethod · 0.80
get_group_byMethod · 0.80

Calls 11

_gen_col_aliasesMethod · 0.95
try_transformMethod · 0.95
setup_joinsMethod · 0.95
get_metaMethod · 0.95
get_initial_aliasMethod · 0.95
trim_joinsMethod · 0.95
FieldErrorClass · 0.90
RefClass · 0.90
getMethod · 0.45
splitMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected