Knowing that QuerySets are lazy, and a db query is made only when we try to access the queryset, I noticed that a db query is not made even if we iteratively try to access queryset object attributes (however, a query is made when we try to access the object itself). So a sample example for demonstration purposes.
from django.db import connection, reset_queries
reset_queries()
tests=Test.objects.using('db').filter(is_test=True)
print(len(connection.queries)) # returns 0
for obj in tests:
print(obj.id)
print(obj.is_test)
print(len(connection.queries)) # returns 0 again
The attributes are correctly printed, but how can they if it shows that a no query was made? Again if we do print(obj)
instead, a query will be made. Any explanation is appreciated.
Edit: This problem arises when we try to query from a nondefault database, which I guess somewhat explains the issue.