How do you define a specific ordering in Django QuerySets?
Specifically, if I have a QuerySet like so: ['a10', 'a1', 'a2'].
Regular order (using Whatever.objects.order_by('someField')) will give me ['a1', 'a10', 'a2'], while I am looking for: ['a1', 'a2', 'a10'].
What is the proper way to define my own ordering technique?
As far as I’m aware, there’s no way to specify database-side ordering in this way as it would be too backend-specific. You may wish to resort to good old-fashioned Python sorting:
If you find yourself needing this kind of sorting a lot, I’d recommend making a custom
models.Managersubclass for your model that performs the ordering. Something like: