[Answer]-Strange ordering result using Django ORM and postgres

1👍

Don’t think the query set is ordered by default. Although it doesn’t seem to be mentioned explicitly in the doc, but this can give some hint

On ordering and order_by()

Warning :

Ordering is not a free operation. Each field you add to the ordering incurs a cost to your database. Each foreign key you add will implicitly include all of its default orderings as well.

So if you have not specified any default ordering field, the queryset will not be ordered.

Sample:

>>> from myapp.models import *
>>> a=MyModel.objects.all()
>>> a.ordered
False
>>> b=MyModel.objects.all().order_by()
>>> b.ordered
False
>>> c=MyModel.objects.all().order_by('id')
>>> c.ordered
True
👤Rohan

Leave a comment