Skip to content
Advertisement

How come queries aren’t being added to Django’s db.connection.queries in tests?

I’m trying to capture the queries which my code submits to the database by examining the contents of django.db.connection.queries. For some reason though, after all the automatically produced setup queries are logged, no further queries are logged from my own code. The following test case demonstrates the behavior.

And here are the results of running it:

Would anyone be able to shed some light on this? Thanks.

Advertisement

Answer

You will not see any queries after executing User.objects.all(). This is only to be expected. The reason? Querysets are lazy. Unless you do something with the queryset NO query will be triggered. To verify this hypothesis, try the following and see if the test passes.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement