How does Django ORM connect to query the database in a loop?

there is a requirement that there is a city field in the MySQL table, and there are 200 cities under that field. Each time, 10 cities are extracted from this field in turn, such as the first 10 cities in the first extraction, 11 ~ 20 in the second extraction, 21 ~ 30 in the third extraction, and then the first one after the 200th extraction. 10 cities are extracted each time to link up the cycle. You cannot use the ID of the field to extract, because cities are often added or deleted. The number of ID does not represent the number of cities. For example, if you delete 100 cities, there are still 200 cities left, but the ID is already 300.

I don"t know how to write it with Django"s ORM. Ask for advice.

Dec.07,2021

this requires saving a state, that is, the current number. You have to keep this value, no matter how you save it. For example, after each request, mark the last item, and then take the values sequentially through the id of the tag.
the real problem to be solved is how to save this value reasonably.
because django is likely to run in multiple processes and cannot communicate between processes, this progress can only be saved to the database or cache or client.
then there is nothing more to say.

Menu