1

I am working on a Django project and using Postgresql as database.

I wanted to know what does sql, params do in the below function which i found at /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

I found it being used at 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 '):]

Since (sql, params) or here (query, params) are not being used then last_executed_query return the same as connections[qs.db][queries][-1]

Santhosh
  • 9,965
  • 20
  • 103
  • 243

1 Answers1

0

Other backends do use sql and params. You can only use connection.queries in debug mode.

Alasdair
  • 298,606
  • 55
  • 578
  • 516