django count sql queries 1

django count sql queries

>>> from django.conf import settings
>>> settings.DEBUG = True
>>> from django.db import connection
>>> Model.objects.count()
>>> # python 3 uses print()
>>> print(len(connection.queries))

Here is what the above code is Doing:
1. We’re importing the django.conf.settings module, and setting DEBUG to True.
2. We’re importing the django.db.connection module, which represents the default database connection.
3. We’re calling Model.objects.count(), which will execute a SQL query.
4. We’re printing the length of connection.queries, which is a list of all SQL queries that have been executed.

Similar Posts