General answer: Django decides to execute the request when you are really requesting some records. Most often, this means repeating the query ( for record in queryset: or using the built-in list() function to convert the query into a list.
See When QuerySets are evaluated for more information from white papers.
This is achieved by defining a class called QuerySet in django/db/models/query.py where special methods like __repr__ , __getitem__ and __iter__ are encoded to work correctly.
If you need to force the download to load, simply run the built-in Python list function in the query set, for example:
qs = SomeModel.objects.all() ql = list(qs)
This call to list() will execute the database query and load all the objects into memory. It should be pretty rare that you need to do this, but in one case you need to use the query results in several places in your templates. Converting to a list and passing the list in the context of your template will execute the request only once, and not once for each place in your template that you iterate.
source share