MCPcopy
hub / github.com/django/django / as_sql

Method as_sql

django/db/models/sql/compiler.py:2036–2111  ·  view source on GitHub ↗

Create the SQL for this query. Return the SQL string and list of parameters.

(self)

Source from the content-addressed store, hash-verified

2034 returning_params = ()
2035
2036 def as_sql(self):
2037 """
2038 Create the SQL for this query. Return the SQL string and list of
2039 parameters.
2040 """
2041 self.pre_sql_setup()
2042 if not self.query.values:
2043 return "", ()
2044 qn = self.quote_name
2045 values, update_params = [], []
2046 for field, model, val in self.query.values:
2047 if hasattr(val, "resolve_expression"):
2048 val = val.resolve_expression(
2049 self.query, allow_joins=False, for_save=True
2050 )
2051 if val.contains_aggregate:
2052 raise FieldError(
2053 "Aggregate functions are not allowed in this query "
2054 "(%s=%r)." % (field.name, val)
2055 )
2056 if val.contains_over_clause:
2057 raise FieldError(
2058 "Window expressions are not allowed in this query "
2059 "(%s=%r)." % (field.name, val)
2060 )
2061 if isinstance(val, ColPairs):
2062 raise FieldError(
2063 "Composite primary keys expressions are not allowed "
2064 "in this query (%s=F('pk'))." % field.name
2065 )
2066 elif hasattr(val, "prepare_database_save"):
2067 if field.remote_field:
2068 val = val.prepare_database_save(field)
2069 else:
2070 raise TypeError(
2071 "Tried to update field %s with a model instance, %r. "
2072 "Use a value compatible with %s."
2073 % (field, val, field.__class__.__name__)
2074 )
2075 val = field.get_db_prep_save(val, connection=self.connection)
2076
2077 quoted_name = qn(field.column)
2078 if (
2079 get_placeholder_sql := getattr(field, "get_placeholder_sql", None)
2080 ) is not None:
2081 sql, params = get_placeholder_sql(val, self, self.connection)
2082 values.append(f"{quoted_name} = {sql}")
2083 update_params.extend(params)
2084 elif hasattr(val, "as_sql"):
2085 sql, params = self.compile(val)
2086 values.append(f"{quoted_name} = {sql}")
2087 update_params.extend(params)
2088 else:
2089 values.append(f"{quoted_name} = %s")
2090 update_params.append(val)
2091 table = self.query.base_table
2092 result = [
2093 "UPDATE %s SET" % qn(table),

Callers 1

execute_returning_sqlMethod · 0.95

Calls 10

pre_sql_setupMethod · 0.95
FieldErrorClass · 0.90
prepare_database_saveMethod · 0.80
extendMethod · 0.80
resolve_expressionMethod · 0.45
get_db_prep_saveMethod · 0.45
appendMethod · 0.45
compileMethod · 0.45
joinMethod · 0.45
returning_columnsMethod · 0.45

Tested by

no test coverage detected