I have a model which has 2 fields
class MyModel:
name = charField()
code = charField(unique=True)
Thats all. no other fields or meta class, nothing.. it's all here.
and in Mysql table I have 8 entries/rows
ID name code
-------------------------
8 Aborted ABT
7 Returned RTC
6 Delivery-Failed DLF
5 Delivered DLV
4 Out-for-Delivery OFD
3 Ready-to-Deliver RTD
2 Order-Placed OPD
1 in transit INT
thats means, the very first entry I did was ("in transit", "INT") then OPD..and so on.
so when I use values_list
on this table it returns
MyModel.objects.values_list('code', 'name')
[(u'INT', u'in transit'), (u'OPD', u'Order-Placed'), (u'RTD', u'Ready-to-Deliver'), (u'OFD', u'Out-for-Delivery'), (u'DLV', u'Delivered'), (u'DLF', u'Delivery-Failed'), (u'RTC', u'Returned'), (u'ABT', u'Aborted')]
which is in expected order, i mean first entry first and last one last.
now when I do MyModel.objects.values_list('code')
it returns
[(u'ABT',), (u'DLF',), (u'DLV',), (u'INT',), (u'OFD',), (u'OPD',), (u'RTC',), (u'RTD',)]
State.objects.values_list('code', flat=True)
returns same result as well.
which seems to be random at first but it seems it's alphabetically sorted.
This is not all, I even tried to pass slicing list in filter
MyMode.objects.filter(code__in=['DLF', 'RTC', 'ABT'][1:]).values_list('code', 'name')
and it again returned some unordered data
[(u'ABT', u'Aborted'), (u'RTC', u'Returned')]
now I want to understand why this is happening. if it's in the code, why django developers did this sorting by default?
PS: I didn't find anything helpful from th doc https://docs.djangoproject.com/en/1.9/ref/models/querysets/#values-list
the code doesn't say anything too https://github.com/django/django/blob/master/django/db/models/query.py#L146
PS: I'm using django 1.9.6 and Mysql Ver 14.14 Distrib 5.5.47