Starting with Django 1.8 , you have conditional expressions , so you no longer need to use extra .
from django.db.models import Case, When, Value, IntegerField SomeModel.objects.annotate( custom_order=Case( When(id=5, then=Value(1)), When(id=2, then=Value(2)), When(id=3, then=Value(3)), When(id=1, then=Value(4)), When(id=4, then=Value(5)), output_field=IntegerField(), ) ).order_by('custom_order')
andilabs
source share