Is there any way to clear django.db.connection.queries?
Question:
I want to monitor query time in my system(built with Django models
).
Finally, I found django.db.connection.queries
.
It shows all queries and time taking for it.
Using this, I want to print the list of which I have done queries at regular interval and then I want to clear the list I printed after printing.
It seems to have methods for a list object(pop, remove
and so on).
But Even though I call pop()
, It doesn’t have any effect, still showing the same length.
How can I clear the list..?
Or Is there any other methods for my intention?
p.s I also found Django-debug-toolbar
but it seems only for view part.
Answers:
You can call reset_queries()
from the django.db module.
from django.db import reset_queries
reset_queries()
You can use reset_queries()
to clear connection.queries
.
For example, if putting reset_queries()
after Person.objects.select_for_update()
as shown below:
# "store/views.py"
from django.db import transaction
from .models import Person
from django.db import reset_queries
from django.db import connection
from django.http import HttpResponse
@transaction.atomic
def test(request):
Person.objects.create(name="John") # INSERT
qs = Person.objects.select_for_update().get(name="John") # SELECT FOR UPDATE
reset_queries() # Here
qs.name = "Tom"
qs.save() # UPDATE
qs.delete() # DELETE
for query in connection.queries: # Here
print(query)
return HttpResponse("Test")
You get only UPDATE
and DELETE
queries without INSERT
and SELECT FOR UPDATE
queries as shown below:
{'sql': 'UPDATE "store_person" SET "name" = 'Tom' WHERE "store_person"."id" = 190', 'time': '0.000'}
{'sql': 'DELETE FROM "store_person" WHERE "store_person"."id" IN (190)', 'time': '0.000'}
[24/Dec/2022 07:00:01] "GET /store/test/ HTTP/1.1" 200 9
I want to monitor query time in my system(built with Django models
).
Finally, I found django.db.connection.queries
.
It shows all queries and time taking for it.
Using this, I want to print the list of which I have done queries at regular interval and then I want to clear the list I printed after printing.
It seems to have methods for a list object(pop, remove
and so on).
But Even though I call pop()
, It doesn’t have any effect, still showing the same length.
How can I clear the list..?
Or Is there any other methods for my intention?
p.s I also found Django-debug-toolbar
but it seems only for view part.
You can call reset_queries()
from the django.db module.
from django.db import reset_queries
reset_queries()
You can use reset_queries()
to clear connection.queries
.
For example, if putting reset_queries()
after Person.objects.select_for_update()
as shown below:
# "store/views.py"
from django.db import transaction
from .models import Person
from django.db import reset_queries
from django.db import connection
from django.http import HttpResponse
@transaction.atomic
def test(request):
Person.objects.create(name="John") # INSERT
qs = Person.objects.select_for_update().get(name="John") # SELECT FOR UPDATE
reset_queries() # Here
qs.name = "Tom"
qs.save() # UPDATE
qs.delete() # DELETE
for query in connection.queries: # Here
print(query)
return HttpResponse("Test")
You get only UPDATE
and DELETE
queries without INSERT
and SELECT FOR UPDATE
queries as shown below:
{'sql': 'UPDATE "store_person" SET "name" = 'Tom' WHERE "store_person"."id" = 190', 'time': '0.000'}
{'sql': 'DELETE FROM "store_person" WHERE "store_person"."id" IN (190)', 'time': '0.000'}
[24/Dec/2022 07:00:01] "GET /store/test/ HTTP/1.1" 200 9