Django - How does order_by work?
As the other answers correctly explain, order_by()
accepts multiple arguments. I'd suggest using something like:
qs.order_by('score','pk') #where qs is your queryset
I recommend using 'pk'
(or '-pk'
) as the last argument in these cases, since every model has a pk
field and its value is never the same for 2 records.
order_by
can have multiple params, I think order_by('score', '-create_time')
will always return the same queryset.
If I understand correctly, I think you need consistently ordered result set every time, You can use something like order_by('score','id')
that will first order by the score first and then by the auto-increment id
within the score
having same values, hence your output being consistent. The documentation is here. You need to be explicit in the order_by if you want to fetch correct result set every time, using 'id' is one of the ways.