我正在研究Django项目,并使用Postgresql作为数据库。
我想知道sql, params
在以下功能中的作用,我在/lib/python3.7/site-packages/django/db/backends/postgresql/operations.py
中找到了
def last_executed_query(self, cursor, sql, params):
# http://initd.org/psycopg/docs/cursor.html#cursor.query
# The query attribute is a Psycopg extension to the DB API 2.0.
if cursor.query is not None:
return cursor.query.decode()
return None
我发现它已在https://stackoverflow.com/a/47542953/2897115
使用def str_query(qs):
"""
qs.query returns something that isn't valid SQL, this returns the actual
valid SQL that's executed: https://code.djangoproject.com/ticket/17741
"""
cursor = connections[qs.db].cursor()
query, params = qs.query.sql_with_params()
cursor.execute('EXPLAIN ' + query, params)
res = str(cursor.db.ops.last_executed_query(cursor, query, params))
assert res.startswith('EXPLAIN ')
return res[len('EXPLAIN '):]
由于未使用(sql, params)
或此处的(query, params)
,因此last_executed_query
返回的内容与connections[qs.db][queries][-1]
相同