[Django]-Order by split string django orm

6👍

Django 2.0 is currently in alpha stage, but it has the StrIndex function that will probably be helpful. I haven’t tested this, but it is a draft of what you can do. The slash will remain in the string, but since you’re just sorting it, I don’t think it will be a problem for you.

MyIDs.objects.filter(run='run_1').annotate(
    slash_pos=StrIndex(F('id'), '/')
).annotate(
    y=Substr(F('id'), F('slash_pos'))
).order_by('y', 'id')
👤Bobort

Leave a comment