Bug #96300 Iterating over a Django QuerySet using a for loop, it "hangs" on last element
Submitted: 23 Jul 2019 22:34
Reporter: Christian Matenaers Email Updates:
Status: Open Impact on me:
Category:Connector / Python Severity:S3 (Non-critical)
Version:mysql_connector_python-8.0.16-cp37-cp37m OS:Mac OS X (10.14.5 (18F132))
Assigned to: CPU Architecture:x86
Tags: hangs, iterator

[23 Jul 2019 22:34] Christian Matenaers
When iterating over a Python (3.7) Django (2.0 - 2.2)  QuerySet, I experience that the iterator hangs on the very last element in the QuerySet.

I have posted details about the issue here: https://stackoverflow.com/questions/57134263/django-paginator-not-responsive-on-last-page

It took me quite a while to find the issue, but I'll explain in the "how to repeat"

How to repeat:
Create a Django ORM model in your DB and add a few records.

Then iterate over the result set like so:

def read_from_db_3():
    res = Skus.objects.filter(status=1)
    for line in res:

You will see that the iterator will continue returning the last element in the list in perpetuity.

This does not happen when you use the .iterator() method, but that may not be an option in certain circumstances where, for example, you rely on a third-party module that doesn't use the iterator and only accepts a QuerySet.