If the ordering field in the inner Meta class of a model is not specified does does the generated SQL query return results with items at randomized positions?
If you do not specify an order (nor through the Meta
, or by an .order_by
clause in the queryset), then Django will construct a query like:
SELECT model.*
FROM model
(perhaps with other clauses, but not with an ORDER BY
statement).
This means that the database has the freedom to return the record in any order. It does not mean that the database will "shuffle" the records, but you simply can not say for sure in what order the records will be retrieved.
Some databases will return the record in (reverse) "edit timestamp" order, so the most recent added/edited records first. But this is not something that you should depend on (you can see this as an implementation detail).
It furthermore can also depend on the query itself if there are JOIN
s involved, since then the table can get ordered due to the JOIN
ing process.
But in general the idea is that it can return the records in any order, and changes to the table, can result in changes in the ordering.
This has some implications on pagination, since if a user moves to the next page, and meanwhile the order changes, the queryset will not per se have the same order when the user fetches the next page. Therefore you better order the queryset (in one way or another). For example by the id.