The best way to get this is to use the debug toolbar , you will also get some additional features for query optimization that will help you optimize your database query.
Here is another solution, you can use connection.queries . This will return the SQL command created for the command that was executed immediately before the connect.queries command. You can execute reset_queries after receiving the time of the previous request using reset_queries() . Using reset_queries() is optional.
Suppose you have a model called Device . You can measure the request time as follows:
>>> from django.db import connection, reset_queries >>> from appname.models import Device >>> devices = Device.objects.all() >>> connection.queries >>> reset_queries()
Faridlu
source share