Skip to main content

CaptureQueriesContext

Context manager that captures queries executed by the specified connection.

Attributes

AttributeTypeDescription
connection[BaseDatabaseWrapper](../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper)The database connection object whose executed queries are being monitored and captured.
force_debug_cursorbooleanStores the original state of the connection's debug cursor setting to ensure it is restored after the context manager exits.
initial_queriesintegerThe count of queries logged by the connection at the start of the context, used as the lower bound for slicing captured queries.
final_queriesinteger = nullThe count of queries logged by the connection at the end of the context, used as the upper bound for slicing captured queries.
reset_queries_disconnectedbooleanA boolean indicating whether the query reset signal was successfully disconnected to prevent query log clearing during capture.
captured_querieslistA property that returns a list of queries executed specifically within the scope of the context manager.

Constructor

Signature

def CaptureQueriesContext(
connection: [BaseDatabaseWrapper](../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper)
)

Parameters

NameTypeDescription
connection[BaseDatabaseWrapper](../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper)The database connection object whose queries will be captured.

Methods


captured_queries()

@classmethod
def captured_queries() - > list

Calculates and returns the subset of queries from the connection log that occurred during this context.

Returns

TypeDescription
listA list of dictionaries representing the SQL queries executed between the enter and exit points